Typoscript issue with GP vars? - typo3

I want to execute a query using typoscript . My query is
SELECT * FROM TABLE where sld like '{GP:SID}' . I want to warp the SID variable inside a single/double quotes. Tried the below script, but didnt worked for me.
lib.products = CONTENT
lib.products {
table = TABLE
select {
pidInList = 26506
orderBy = name
where = sid like '{GP:SID}'
}
renderObj = COA
renderObj {
10 = COA
10 {
10 = TEXT
10.dataWrap ={field:name}[\n]
}
}
}
Can any help me with this ?

You code is insecure! Don't use it on production.
What you should do instead - is to use TS query markers, which uses prepared statements in a background.
lib.products = CONTENT
lib.products {
table = TABLE
select {
pidInList = 26506
orderBy = name
where = sid like '###sid###'
markers {
sid.data = GP:SID
}
}
renderObj = COA
...
}
Also, if you want to use LIKE, you need % sign to make it work, otherwise it is same as 'equals', but slower. However, I'm not sure, what happens, if GP:SID contains % sign at the beginning or at the end of a string.

Related

Pass Fluid variable to TypoScript

I want to pass a variable (uid of category) in Fluid to a TypoScript :
<f:cObject typoscriptObjectPath="lib.testFluid" data="{setting.myvar}/>
Then i want to use the var to get all content elements in folder with pid 942 and the category {setting.myvar}
lib.testFluid = COA
lib.testFluid = CONTENT
lib.testFluid {
table = tt_content
select {
pidInList = 942
where = selected_categories = |
}
}
This does not work, it creates an MySql syntax error. I also tried using current = 1 instead of the where clause without success. I looked at post TYPO3: pass variable to typoscript via cObject? and I can recreate it but it does not work with my script. (TYPO3 8)
If i use
...
where = selected_categories = 13
....
the scrip will succesfully display all CE with category 13. How do i make it work with a var?
could you try this:
<f:cObject typoscriptObjectPath="lib.testFluid" data="{myvar: setting.myvar}/>
lib.testFluid = CONTENT
lib.testFluid {
table = tt_content
select {
pidInList = 942
where.data = field:myvar
where.intval = 1
where.wrap = selected_categories=|
}
}
hard to test for me but it might work ...
I had to solve it once with markers. I couldn't find another simpler way. I give you a very general solution which you may adapt to your needs. For example you could set the pid value via a typoscript setting which is more elegant than to put it in the snippet code. Please try:
<f:cObject typoscriptObjectPath="lib.testFluid" data="{category: setting.myvar, catPid: 942}" currrentValueKey="category" />
The related TypoScript snippet:
lib.testFluid = COA
lib.testFluid {
10 = LOAD_REGISTER
10 {
category.cObject = TEXT
category.cObject.value.current = 1
catPid.cObject = TEXT
catPid.cObject.value.dataWrap = { field: catPid }
}
20 = CONTENT
20 {
table = tt_content
select {
pidInList.cObject = TEXT
pidInList.cObject.dataWrap = {REGISTER:catPid}
where = selected_categories=###category###
markers {
category.data = REGISTER:category
}
}
}
30 = RESTORE_REGISTER
}

fallback for FILES in typoscript

I'm using typoscript go get the first file out of a page. This works and is out of the documentation. But the documentation doesn't tell something about a fallback if no files are found.
lib.files = FILES
lib.files {
references {
table = pages
fieldName = media
}
file =
begin = 0
maxItems = 1
renderObj = TEXT
renderObj {
value = URL: {file:current:publicUrl}
insertData = 1
}
}
A naive .if.isNull failed at the first attpent. Is there a possibilty to define a fallback if the FILES-Object is empty?
Mh,
try to make a second object and check there if lib.files is filled with data.
__
UNTESTED
lib.files = FILES
lib.files {
references {
table = pages
fieldName = media
}
file =
begin = 0
maxItems = 1
renderObj = TEXT
renderObj {
value = URL: {file:current:publicUrl}
insertData = 1
}
}
lib.fileFallback = COA
lib.fileFallback {
10 < lib.files
20 = TEXT
20 {
value = Do something
stdWrap.if.isFalse.cObject < lib.fileFallback.10
}
}
I am using this technique often to slide through pages for slideshow objects.
Let me know, if it is working.
You should check the TypoScript Reference stdWrap.ifEmpty.
In your case the code should look like this
lib.files {
# ....
stdWrap.ifEmpty.cObject = IMAGE
stdWrap.ifEmpty.cObject{
file = fileadmin/.../my_image.png
}
# ....
}
You can use every cObject you want.
Check out Content Objects (cObject)

how to insert the language id in a typolink in typoscript

i want to add the language id in a typolink
so far
LOGO = COA
LOGO {
10 = TEXT
10 {
value = logo
typolink {
parameter = 116
additionalParams = &L={$config.sys_language_uid}
}
}
}
if L=4 it's working
but if i use L={$config.sys_language_uid} it gets ignored altogether
same with L=GP:L
and L=GPvar:L
what would be the proper syntax here
working if i do something like
additionalParams = COA
additionalParams {
10 = TEXT
10.data = GP : L
10.intval = 1
10.wrap = &L=|
}
You do not need to do that on your own. With the following global TypoScript configuration the parameter L will added to every link:
config.linkVars = L(int)
So, if you use HMENU.special = language this will be managed on the switch automatically, too:)
http://docs.typo3.org/typo3cms/TyposcriptReference/Setup/Config/Index.html
I do not know about {$config.sys_language_uid}, but your code will output it as plain text. In order to use variables like {GP:L}, you got to dataWrap it or insert "insertData" after the value.
10 = TEXT
10 {
value = logo
typolink {
parameter = 116
additionalParams.dataWrap = &L={GP:L}
}
}
OR (the best way I'd say):
10 = TEXT
10 {
value = logo
typolink {
parameter = 116
additionalParams.cObject = TEXT
additionalParams.cObject {
wrap = &L=|
data = GP:L
}
}
}
Using {TSFE:sys_language_uid} might be a better option if you are querying the database.
There is no need to make a cObject or COA
additionalParams = type=0&L={GP:L}
additionalParams.insertData = 1
You can do this kind of URL using addQueryString = 1. https://docs.typo3.org/typo3cms/TyposcriptReference/Functions/Typolink/Index.html#addquerystring

Result of SQL to Userfunc in TypoScript

I am very new to Typo3/TypoScript and I wondered if and how it is possible to give the result of a sql-statement in TypoScript to a php function.
I've managed the userFunc so far, but i'm getting confused with the rest.
Here is my try:
temp.pidList = CONTENT
temp.pidList {
table = tx_eepcollect_sessions
#table = tt_content
select {
pidInList < plugin.tx_eepcollect_pi1.pid_list
where {
stdWrap.cObject = TEXT
stdWrap.cObject {
data = global : _COOKIE | tx_eepcollect_pi1
wrap = ses_id='|'
}
}
}
renderObj = COA
renderObj {
10 = TEXT
10.field = ses_data
#30 = TEXT
#30.data = debug:data
}
}
includeLibs.user_idList = fileadmin/services/user_IdList.php
temp.ListOfIds = USER
temp.ListOfIds.userFunc = user_IdList->get_IdList
#temp.pidList = TEXT
#temp.pidList = {"1275":{"id":"1275","tx_eepcollect_pi1":{"prozess":"add","pid":"1275","ctrl":"1360858765"},"cHash":"e90b62584f3f0e4f71bf1100faf39d83"}}
temp.ListOfIds.userFunc.jsonList < temp.pidList
temp.mainContent = COA_INT
temp.mainContent.10 = TEXT
temp.mainContent.10.stdWrap.cObject < temp.ListOfIds
The output is an array of a lot of stuff, but not the result of the database-query.
TypoScript is not a programming language, so it is not executed in any meaning. You should think about TS as set of instructions for the Core.
So, following line:
temp.ListOfIds.userFunc.jsonList < temp.pidList
will not put result of temp.pidList into temp.ListOfIds.userFunc.jsonList, as you would expect from a programming language - it will simply copy set of instructions, so at the end you'll have following structure:
temp.ListOfIds = USER
temp.ListOfIds.userFunc = user_IdList->get_IdList
temp.ListOfIds.userFunc.jsonList = CONTENT
temp.ListOfIds.userFunc.jsonList.table = tx_eepcollect_sessions
[...]
And since jsonList is your custom property you need to bring stdWrap to it by following method:
TypoScript
temp.ListOfIds.userFunc.jsonList.cObject < temp.pidList
PHP
$jsonList = $this->cObj->stdWrap($conf['jsonList'], $conf['jsonList.']);
I assume, that you call it from a method get_IdList of class user_IdList with two params: $content and $conf.
As an additional measerue, you can make it as extension with a kickstarter or extension builder, so it'll be easier for you to work with configuration.
Another thing is, that your code have potential SQL injection vulnerability here:
where {
stdWrap.cObject = TEXT
stdWrap.cObject {
data = global : _COOKIE | tx_eepcollect_pi1
wrap = ses_id='|'
}
}
So, you may consider reading about markers

Typo3: display content from first sub-page using typoscript

This is what I want to do: on a given page, I want to display all content elements of the first child page of a given page. I cannot simply use a shortcut page, because I need to display other content elements after the ones from the sub-page. How can I do this?
Here is a snippet of how I think I could do it, but I don't know how to build the select. Is there a better way?
# save current content
tmp.pagecontent < page.10.subparts.main-content
# clear the content of the main column
page.10.subparts.main-content >
# build a new object for this column as content-object-array
page.10.subparts.main-content = COA
page.10.subparts.main-content {
10 = CONTENT
10.table = tt_content
10.select {
# what should I put here?
}
# re-insert the normal pagecontent to the page
20 < tmp.pagecontent
Just add answer for other people.
First : specify the first sub page of current page.
Second : get content elements that you want of that sub page.
temp.content = COA
temp.content {
10 = CONTENT
10 {
table = pages
select {
pidInList.field = uid
orderBy = sorting ASC
max = 1
begin = 0
}
renderObj = COA
renderObj {
10 = CONTENT
10 {
table = tt_content
select {
languageField = sys_language_uid
pidInList.field = uid
orderBy = sorting
#where = colPos = 10
}
stdWrap.wrap = |
}
}
}
}
I finally succeed! Not sure though it is the best way. What do you think about it? Should I put the second select into userFunc too?
fileadmin/userfunc/mailArchive.php
<?php
class user_mailArchive {
function getFirstChild($content, $conf) {
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
'uid', // SELECT ...
'pages', // FROM ...
'pid='.intval($conf['pid']), // WHERE...
'', // GROUP BY...
'sorting', // ORDER BY...
'1' // LIMIT ...
);
$row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
if ($row) {
return $row['uid'];
}
else {
return '';
}
}
}
TS template
# fill the content of the main-column to a tmp.object
tmp.pagecontent < page.10.subparts.main-content
# clear the content of the main column
page.10.subparts.main-content >
includeLibs.mailArchive= fileadmin/userfunc/mailArchive.php
# build a new object for this column as content-object-array
page.10.subparts.main-content = COA
page.10.subparts.main-content {
10 = CONTENT
10 {
table = tt_content
select {
pidInList.cObject = USER
pidInList.cObject {
userFunc = user_mailArchive->getFirstChild
# parent page ID
pid = 139
}
orderBy = sorting
}
}
# re-insert the normal pagecontent to the page
20 < tmp.pagecontent
}