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(...);
}
}