TYPO3/Typoscript : render sql query as array - typo3

I am using TYPO3 6.2.
On my website, i make a SQL query this way :
lib.bloc_top = COA
lib.bloc_top.10 < styles.content.get
lib.bloc_top.10.select.selectFields = header
lib.bloc_top.10.select.where = ( deleted = 0 && hidden = 0 && tx_gridelements_container = 2571 && CType = 'header' )
All works but instead of outputting the results as HTML code, I would like to render it in an array that I would use in my FLUID template this way :
<f:for each="{car}" as="el">
<li>Brand : {el}</li>
</f:for>
Is it possible ?
Thanks for your help :)

You should not select the container by a specific UID, but create a "car" container type via Gridelements CE backend layout instead.
Anyway, to get the data into an array, you don't have to do anything special, since this is automatically done by styles.content.get under the hood.
While styles.content.get uses the default tt_content setup, you can change that via renderObj as described here: https://docs.typo3.org/typo3cms/TyposcriptReference/6.2/ContentObjects/Content/
To get the data into your Fluid template you just have to replace the default renderObj with a FLUIDTEMPLATE https://docs.typo3.org/typo3cms/TyposcriptReference/6.2/ContentObjects/Fluidtemplate/Index.html
lib.bloc_top.10.renderObj = FLUIDTEMPLATE
lib.bloc_top.10.renderObj {
file = path/to/your/template/file.html
}
Since the loop is handled by the CONTENT object of styles.content.get, you can skip the f:for part in your template though.
Usually any kind of data is provided within the cObj->data array, so something like <h1>{data.header}</h1> should do the job.
And even with CONTENT there should be a counter, since there is https://docs.typo3.org/typo3cms/TyposcriptReference/DataTypes/Gettext/Index.html#cobj
To get any available data you should use <f:debug>{_all}</f:debug> in your Fluid template.

Related

How to prevent TYPO3 to render head- and body-elements when outputting JSON?

In TYPO3 8.7 I'm trying to generate JSON using FLUID.
I've created a page with dummy content and I've updated my TypoScript configuration for all pages in a folder.
TypoScript setup
[PIDinRootline = 10]
page = PAGE
page {
typeNum = 0
config {
disableAllHeaderCode = 1
disablePrefixComment = 1
xhtml_cleaning = none
admPanel = 0
debug = 0
metaCharset = utf-8
additionalHeaders = Content-Type:text/json;charset=utf-8
}
}
[global]
I created a dummy JSON file as well, to test the output, before creating the actual content using FLUID:
api.json
{
"hello": "world"
}
Now, this seems to work fine. But the output still includes the html- and body-elements.
Rendered output:
<html>
<head></head>
<body>
{"hello": "world"}
</body>
</html>
The documentations says:
If you want to output JSON, RSS or similar data with Fluid,
you have to write the appropriate TypoScript which passes the
page rendering to Extbase and Fluid respectively. Otherwise,
TYPO3 will always generate the <head>- and <body>-section.
But I don't know what to do here actually. How can I get TYPO3 to not render the wrapping HTML-elements?
The problem was a new syntax in TYPO3 8 and newer. In newer versions, the additionalHeaders is no longer just a TEXT but an array with numeric indices. So to set the correct header type, you have to use this one:
additionalHeaders.10.header = Content-Type:application/json;charset=utf-8
When you override the page object, you should remove all inside it before with page >.
Additionally Fluid is a templating engine for HTML, I don't know if that fits for JSON output.
Better way:
I'd suggest to use a separate object than page and a different page type:
[globalVar = GP:type = 133]
jsonOutput = PAGE
jsonOutput {
typeNum = 133
config {
...
}
}
[global]
What you are missing is this typoscript:
config.disableAllHeaderCode = 1
read the manual
as you want it on a special page you can use a special typoscript template for this page. For a pagetype it is a little more complicated as this configuration would work on all pagetypes.
if you want some config options only for a special pagetype you can add this config to the pagetype only like this:
json = PAGE
json.config.disableAllHeaderCode = 1
Be aware that fluid is more complicated if you output other structure than HTML. But it is possible! Have a special look on whitespace and braces ({})

How to output COA_INT in fluid template form element

I'd like to pass an extension GP variable to a form that I built outside of the plugin. When trying to do so, I used a COA_INT which turned out to output <INT_SCRIPT. instead of the value.
In order to do so, I built this typoscript object:
lib.gpSWord = COA_INT
lib.gpSWord {
# Protects from XSS!
stdWrap.htmlSpecialChars = 1
10 = TEXT
10 {
stdWrap.data = GP:tx_indexedsearch_pi2|search|sword
}
}
and then tried to output it like this (as suggested here:
<f:form.textfield name="search[sword]" value="<f:format.raw>{f:cObject(typoscriptObjectPath: 'lib.gpSWord')}</f:format.raw>" id="tx-indexedsearch-searchbox-sword" class="tx-indexedsearch-searchbox-sword font-h2" />
And also tried building up the variable before hand using f:variable but nothing worked.
The expected result would be that the variable would be set and the value would then be passed to the form.textfield viewhelper but appearently this does'nt work either.
You should commit the variable in Typoscript (setup) with:
page.10 = FLUIDTEMPLATE
page.10.variables.gpSWord < lib.gpSWord
Then you are able to use it in you Fluidtemplate with:
<f:format.raw>{gpSWord}</f:format.raw>
lib.gpSWord = COA_INT
lib.gpSWord {
stdWrap {
wrap = &tx_indexedsearch_pi2[sword]=|
data = GP:tx_indexedsearch_pi2|sword
if.isTrue.data = GP:tx_indexedsearch_pi2|sword
}
}
Try this for GP.

TYPO3 CASE with Templavoilà FCE id

Is it possible to access the id of a Templavoilà Data Structure / FCE for a CASE ?
I tried this which didn't work :
10 = CASE
10 {
key.field = tx_templavoilaplus_datastructure
100 = TEXT
…
default = TEXT
…
}
Thank you ! =)
The field for the Data Structure in tt_content for TemplaVoila is tx_templavoila_ds. If you're using TemplaVoila Plus it's tx_templavoilaplus_ds.
Also, only the fields in the data structure are available directly. However, TemplaVoila loads the parent record data in the register. You can access it using key.data = register:tx_templavoila_pi1.parentRec.tx_templavoila_ds. For TemplaVoila Plus it's key.data = register:tx_templavoilaplus_pi1.parentRec.tx_templavoilaplus_ds

Build HMENU with Information from TCE

I have extended the TCA for every Backend-Page on the Page-Tree. One of the new Options is the "Page-Type", for example "PressPage". With this Extension, i have a new databasefield in the table "pages".
Now i would build an HMENU/TMENU with all pages, below this folder.
[...]
lib.MetaPressNavigation{
special = directory
special.value = ID_FROM_FOLDER_WITH_PAGETYPE_PRESSPAGE
[...]
But i have no idea to realize them with typoscript.
I hope anyone can help me.
Thanks.
EDIT:
Now - i have try it with an extended TCA. It's very easy for any User to make some configuration for this projectpage. The Users can set a value with an Checkbox in a special tab.
I have try to get the page out form the database, with this special config - any page have in the database on the column "tx_meta_pagetype the value 9. I need the UID from this page to build the META-Navigation. It will be full functional - when i give a hardcoded uid, but i need this dynamic.
This is my attemp, to get the UID from the database:
temp.MetaNavigationIds = CONTENT
temp.MetaNavigationIds{
table = pages
select.Where = tx_meta_pagetype = 9 #tx_meta_pagetype is set from the TCA
renderObj = TEXT
renderObj.field = uid
renderObj.stdWrap = |
}
lib.MetaNavigation = HMENU
lib.MetaNavigation{
special = directory
special.value < temp.MetaNavigationIds #the UID of configured page, that i need for the menu
1 = TMENU
1 {
wrap = <ul> | </ul>
NO{
wrapItemAndSub = <li> | </i>
wrapItemAndSub.insertData = 1
allStdWrap.insertData = 1
}
}
}
I have try a lot of database question with typoscript, but nothing works.
Be careful, special.value is not a content object but just a property. You are copying a content object (CONTENT) in its place. This does not work.
However it does have stdWrap. Therefore something like
special.value.stdWrap.cObject < temp.MetaNavigationIds
Should work out.
For renderObj.stdWrap = | enter renderObj.wrap = |, instead. Please mind the trailing comma. This will make sure that you actually get a comma separated list of uids. Otherwise your uids would be printed just after each other, thus forming one big number.
Please test each part individually before adding the components together. You should make sure that each pease returns the correct data, otherwise you will never get a working solution.
And of course select.Where must be select.where. Capitalization does matter.
Here is a working example for CONTENT:
page.10 = CONTENT
page.10 {
table = pages
select {
where = doktype = 199
recursive = 99
# Needs to be your root page uid
pidInList = 1
}
renderObj = TEXT
renderObj.field = uid
renderObj.wrap = |,
}
If you are using TYPO3 6.2, I would recommend to use the new category system. You can create different categories in the TYPO3 backend and assign those categories to your pages.
With this, you can create a HMENU/TMENU like shown below:
20 = HMENU
20 {
special = categories
special.value = 1,2
1 = TMENU
1.NO {
...
}
}
If you do not use TYPO3 6.2 or do not want to use the category system, you can use a userfunction to return the pages which matches your "Page-Type".
HMENU/TMENU TypoScript will be like shown below.
lib.leftmenu.20 = HMENU
lib.leftmenu.20.special = userfunction
lib.leftmenu.20.special.userFunc = user_myspecialmenu_pi1->getPressPages
The TypoScript above is just an example, and you need to code the userfunction your own.
A detailed reference of the special property userfunction is available here and a example can be found here.

Generating random string by TypoScript

Is it possible to auto generate a mixed string of digits and letters by TypoScript, e.g. 12A54 or something similar?
I'd prefer a userFunc to a php include script. For example, you can pass parameters to a user function.
Typoscript:
includeLibs.generateInvoiceNo= fileadmin/scripts/generateInvoiceNo.php
temp.invoiceNo = USER
temp.invoiceNo {
userFunc =user_generateInvoiceNo->main
}
PHP:
fileadmin/scripts/generateInvoiceNo.php
<?
class user_generateInvoiceNo {
var $cObj;// The backReference to the mother cObj object set at call time
/**
* Call it from a USER cObject with 'userFunc = user_generateInvoiceNo->main'
*/
function main($content,$conf){
$length = 6;
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$number=substr(str_shuffle($chars),0,$length);
return $number;
}
}
?>
Credits:
This php script
TYPO3 Wizard on userFunc
As already mentioned, there is no such functionality in Typoscript and so the preferred method is to use some simle PHP function as suggested in other answers.
However, there is a cheat and that would be to use MySQL. Mind you, that it's a solution only if you absolutely cannot (for a reason that I really cannot think of) write a piece of custom PHP. Take it rather as an academic answer than a practical one.
temp.random = CONTENT
temp.random {
table = tt_content
select {
pidInList = 1
recursive = 99
max = 1
selectFields = SUBSTRING(MD5(RAND()) FROM 1 FOR 6) AS random_string
}
renderObj = TEXT
renderObj {
field = random_string
case = upper
}
}
NOTES:
pidInList must point to an existing page.
The MySQL command is really just an example as the string would never contain letters G-Z. I'm sure it's possible to come up with a better string generated by MySQL.
Patching TYPO3 sources for such easy tasks is wrong idea. After the next source upgrade you'll lose your changes.
Instead it's better to include an easy PHP script where you can render what you need check TSREF