How to use variable/user_func in a conditional statement in Typoscript (TYPO3 6.2)?

602 Views Asked by At

I want to use a conditional statement for applying a different template to News (tt_news). Currently I use a user function that returns true/false. If the current News has a specific category and works correctly return 1 otherwise return Null.

I followed the official documentation and other sites, so I made the condition

[userFunc = user_isLatin]
 plugin.tt_news.templateFile = fileadmin/templates/plugins/tt_news/latin_detail.html
 page.1010 = TEXT
 page.1010.value = LATIN  
[ELSE]
 plugin.tt_news.templateFile = fileadmin/templates/plugins/tt_news/general_detail.html
 page.1010 = TEXT
 page.1010.value = OTHERS      
[END]

but it always shows OTHERS. I tried the following with variables

temp.catuid = USER
temp.catuid.preUserFunc = user_ttNewsInCat

latin = TEXT
latin.value < temp.catuid

[latin.value = 1]
 ....
[ELSE]
 ....
[END]

but it doesn't work either.

2

There are 2 best solutions below

1
Artur Cichosz On

It works exactly like you tried to do it. This is the cobndition I tested now:

[userFunc = user_isLatin]
 page.10 = TEXT
 page.10.value = LATIN
[ELSE]
 page.10 = TEXT
 page.10.value = OTHERS      
[END]

And this is the implementation of the user function which must be in the AdditionalConfiguration.php or the localcon.php file of your custom extension (I suppose this is what you missed).

function user_isLatin() {
    return TRUE;
}

For more details see the offcial documentation: https://docs.typo3.org/typo3cms/TyposcriptReference/6.2/Conditions/Reference/Index.html#userfunc

2
Thomas Löffler On

You don't need to use a user_ function in 6.2, you can also use a class. And you don't need to define it in AdditionalConfiguration.php.

TypoScript:

[userFunc = Vendor\ExtName\Condition\TypoScriptCondition::isLatin()]
...
[else]
...
[global]

PHP:

<?php
namespace Vendor\ExtName\Condition;

class TypoScriptCondition
{

    public static function isLatin()
    {
        ...
        return true;
    }
}