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).
$GLOBALS['TYPO3_REQUEST']
?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.
My code snippets show different unsuccessful attempts (v1, v2) of accessing the request object in tt_content.php and in my class method.
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;
}
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(...);
}
}
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