TYPO3 TCA display condition (displayCond) with mysql to MM table

2.3k views Asked by At

I have an extension with a main table with a checkbox which should not be available if the item already has a relation through an MM table, the relative TCA :

'checkbox' => [
  'displayCond' =>'FIELD:uid:!IN:SELECT uid_foreign FROM tx_myext_object_object_mm',
  'exclude' => 0,
  'label' => 'checkbox',
  'config' => [
    'type' => 'check',
    'items' => [
      '1' => [
        '0' => 'LLL:EXT:lang/locallang_core.xlf:labels.enabled'
      ]
    ],
  'default' => 0
  ]
],

can this syntax be corrected or is it impossible (this snippet does not work)

1

There are 1 answers

1
jokumer On BEST ANSWER

Since TYPO3 7.6 userFunc is available as display Condition.

In your case I recommend for your TCA configuration:

'checkbox' => [
    'displayCond' =>'USER:VendorName\\Myext\\DisplayConditionMatcher->displayIfTxMyextObjectHasNoMMRelation',
    'exclude' => 1,
    'label' => 'Checkbox',
    'config' => [
        'type' => 'check',
        'default' => 0
    ]
],

And a PHP class named DisplayConditionMatcher.php located in your extension EXT:myext/Classes/ with following content:

<?php
namespace VendorName\Myext;

/**
 * Class DisplayConditionMatcher
 *
 * @package TYPO3
 * @subpackage tx_myext
 * @author 2016 J.Kummer <typo3 et enobe dot de>
 * @copyright Copyright belongs to the respective authors
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 or later
 */

class DisplayConditionMatcher {

    /**
     * Checks for already existing mm relation of tx_myext_object
     * Returns true, if no mm relation found
     * 
     * @param array $configuration
     * @param \TYPO3\CMS\Backend\Form\FormDataProvider\EvaluateDisplayConditions $evaluateDisplayConditions
     * @return bool
     */
    public function displayIfTxMyextObjectHasNoMMRelation(array $configuration, $evaluateDisplayConditions = null)
    {
        $result = true;
        if (isset($configuration['record']['uid'])) {
            $countRows = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows(
                'uid_foreign',
                'tx_myext_object_object_mm',
                'uid_foreign = ' . intval($configuration['record']['uid'])
            );
            if ($countRows > 0) {
                $result = false;
            }
        }
        if (isset($configuration['conditionParameters'][0]) && $configuration['conditionParameters'][0] === 'negate') {
            $result = !$result;
        }
        return $result;
    }
}

You can pass additional parameters separated by colon for displayCondition of type userFunc, as described in TYPO3 CMS TCA Reference. For example negation, as already implemented in PHP class:

'displayCond' =>'USER:VendorName\\Myext\\DisplayConditionMatcher->displayIfTxMyextObjectHasNoMMRelation:negate',

Adapt names for extension, path and vendor that matches your needs.