0

I’m in TYPO3 11.5. In my sitepackage extension I need to access the TYPO3 request object in either Configuration/TCA/Overrides/tt_content.php or a custom class method. But I don’t know how to instantiate the PSR-7 request object nor is the old style $GLOBALS['TYPO3_REQUEST'] available in either of the two files (I get the warning that it’s undefined).

Questions:

  • How can I instantiate the PSR-7 request object in either the TCA override or my custom class?
  • If that’s not possible, why can’t I get access to $GLOBALS['TYPO3_REQUEST']?

Background:

In Configuration/TCA/Overrides/tt_content.php of my sitepackage extension I define several custom content elements. To make it a little easier I write their configuration (CType, showItem, flexform, …) into an array and then iterate through it, calling a custom class method. This class method calls the ExtensionManagementUtility methods addTcaSelectItem and addPiFlexFormValue for each content element.

That all works. But now I want to add a condition based on the site object (my sitepackage should run on two different sites) and I try to get the site information using the TYPO3 request object. The documentation on the TYPO3 request object tells me that I need to pass it as an argument to a user function – but it does not tell me how to instantiate it.

Code snippets:

My code snippets show different unsuccessful attempts (v1, v2) of accessing the request object in tt_content.php and in my class method.

Snippet of Configuration/TCA/Overrides/tt_content.php:

// ... defining associative array $ce with CType names as key ...
// then calling my class method on each $ce element:

$addCEObj = new \Vendor\Sitepackage\Utility\AddContentElement;
$addAfter = 'textmedia';
foreach ($ce as $CType => $tcaConf) {
    // $tcaConf contains 'showItem', 'colOverrides', 'flexform' and the localised title
    $tcaConf['CType'] = $CType;
    $tcaConf['addAfter'] = $addAfter;
    /* (v1a) */ $addCEObj->tca($tcaConf, $request);
    /* (v1b) */ $addCEObj->tca($tcaConf, $GLOBALS['TYPO3_REQUEST']);
    /* (v2)  */ $addCEObj->tca($tcaConf);
    $addAfter = $CType;
}

Snippet of Classes/Utility/AddContentElement:

class AddContentElement
{
    /**
     * used for v2
     */
    public function __construct()
    {
        $this->request = $GLOBALS['TYPO3_REQUEST'];
    }

    /* (v1) */ public static function tca(array $tcaConf, ServerRequestInterface $request)
    /* (v2) */ public function tca(array $tcaConf)
    {
        /* (v1) */ $site = $request->getAttribute('site');
        /* (v2) */ $site = $this->$request->getAttribute('site');
        $siteId = $site->getIdentifier();
        
        // ... adding the content element to the TCA based on site identifier using:
        ExtensionManagementUtility::addTcaSelectItem(...);
        ExtensionManagementUtility::addPiFlexFormValue(...);
    }
}
clive beckett
  • 152
  • 16

1 Answers1

2

TCA is generated once then and cached. accessing the request. is a bad idea in this context. there might not even a request if the first instance which needs TCA is CLI command.

so therefore i would strongly suggest not to use this king of thing. but rather use TSconfig to disable the content elements you don' want on a certain site.

https://docs.typo3.org/m/typo3/reference-tsconfig/main/en-us/PageTsconfig/TceForm.html#removeitems

Wolffc
  • 1,176
  • 6
  • 9
  • Thank you! This works … even though it feels weird, to first add CEs and then remove them again. – clive beckett Feb 15 '23 at 13:44
  • TS config is more on the line of setting policies for users or pages. so you system is capable of having all these content elements but a users is not allowed to use all of the capabilities (in all places) – Wolffc Feb 24 '23 at 09:55