Search code examples
typo3extbasetypo3-9.xtypo3-extensions

enablecolumns and QueryBuilder in TYPO3 9.5


I have a question about enablecolumns and QueryBuilder in TYPO3 9.5:

In the TCA I defined the enablecolumns:

    'ctrl' => [
      'title'       => '....'
      'tstamp'          => 'tstamp',
      'crdate'          => 'crdate',
      'versioningWS'        => 2,
      'versioning_followPages'  => true,
      'origUid'             => 't3_origuid',
      'languageField'   => 'sys_language_uid',
      'transOrigPointerField'   => 'l18n_parent',
      'transOrigDiffSourceField'    => 'l18n_diffsource',
      'delete'          => 'deleted',
      'enablecolumns' => [
        'disabled' => 'hidden',
        'fe_group' => 'fe_group',
      ],
    ]

In the repository I create a custom Query:

    $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_myext_domain_model_table'); 
    $result = $queryBuilder
      ->select('*')
      ->from('tx_myext_domain_model_table')
      ->where($where)
      ->execute();

If I debug the SQL with

$queryBuilder->getSQL();

I see the deleted, and hidden conditions but no fe_group

If I add

$queryBuilder
->getRestrictions()
->removeAll()
->add(GeneralUtility::makeInstance(FrontendGroupRestriction::class))
->add(GeneralUtility::makeInstance(DeletedRestriction::class))
->add(GeneralUtility::makeInstance(HiddenRestriction::class));

before the execution of the query, the fe_groups condition ist added.

What am I missing?

Thank you Christian


Solution

  • This is because the QueryBuilder by default uses the DefaultRestrictionContainer which adds only the following restrictions:

    protected $defaultRestrictionTypes = [
        DeletedRestriction::class,
        HiddenRestriction::class,
        StartTimeRestriction::class,
        EndTimeRestriction::class
    ];
    

    References:

    What you're probably looking for is the FrontendRestrictionContainer which uses the following default restrictions:

    protected $defaultRestrictionTypes = [
        DeletedRestriction::class,
        FrontendWorkspaceRestriction::class,
        HiddenRestriction::class,
        StartTimeRestriction::class,
        EndTimeRestriction::class,
        FrontendGroupRestriction::class,
    ];
    

    Reference: https://github.com/TYPO3/TYPO3.CMS/blob/9.5/typo3/sysext/core/Classes/Database/Query/Restriction/FrontendRestrictionContainer.php#L33-L40

    A possible solution would be to use this container instead of the default one:

    $container = GeneralUtility::makeInstance(FrontendRestrictionContainer::class);
    $queryBuilder->setRestrictions($container);