Search code examples
typo3tca

TYPO3 Display Cond AND within OR not working


I am trying to combine some conditions to display a field. I want to show the field if (CType is text AND layout is 1) OR (Ctype is textpic AND layout is 2), but it doesn't work. It enters only the second condition when CType is textpic AND layout is 2.

Here is my code:

'displayCond' => [
  'OR' => [
    'AND' => [
      'FIELD:layout:=:1',
      'FIELD:CType:=:text',
    ],
    'AND' => [
      'FIELD:layout:=:2',
      'FIELD:CType:=:textpic',
    ],
  ],
],

Can someone help out?


Solution

  • In general, combining and nesting display conditions works like a charm. But the boolean operators (AND and OR) to combine several conditions have to be defined as array keys, which makes it impossible to have the same operator more than once on the same nesting level as Julian Hofmann already pointed out.

    Your definition results in:

    'displayCond' => [
      'OR' => [
        'AND' => [
          'FIELD:layout:=:2',
          'FIELD:CType:=:textpic',
        ],
      ],
    ],
    

    Solution

    To solve your problem or in general, to work with more complex conditions, you can use a custom userFunc class as explained in the displayCond documentation. You can create custom conditions in PHP and return whether the field should be displayed (true) or not (false).

    Example user function

    <?php
    
    declare(strict_types=1);
    
    namespace Your\Namespace\UserFuncs;
    
    class DisplayCondMatcher
    {
        public function checkCTypeAndLayout(array $params): bool
        {
            $CType = $params['record']['CType'];
            $layout = $params['record']['layout'];
    
            if (
                ($CType === 'text' && $layout === 1)
                || ($CType === 'textpic' && $layout === 2)
            ){
                return true;
            }
    
            return false;
        }
    }
    

    Example usage:

    'displayCond' => 'USER:Your\\Namespace\\UserFuncs\\DisplayCondMatcher->checkCTypeAndLayout',