Reputation: 29
Previously in version 8 and 9, I could access the page id using the following
$currentPid = $GLOBALS['TSFE']->id
I am now updating my plugin to support 10.4, the above snippet gives out a null value. How can I access the page id and then also the root of the page id. I need the root page to gather the typoscript info from the root template.
$currentPage = $GLOBALS['TSFE']->id;
if ($currentPage === null) {
$localTSFE = clone $GLOBALS['TSFE'];
if (version_compare($typo3Branch, '9.5', '>=')) {
$localTSFE->fe_user = GeneralUtility::makeInstance(FrontendUserAuthentication::class);
}
$localTSFE->determineId();
$currentPage = $localTSFE->id;
}
$rootLine = GeneralUtility::makeInstance(RootlineUtility::class, $currentPage)->get();
$templateService->start($rootLine);
$setup = $templateService->setup;
return $setup;
After the first line the currentPage is being set to null.
Then an error is being thrown when cloning the $GLOBALS['TSFE'].
Error: _clone on non-object
Upvotes: 2
Views: 4516
Reputation: 3230
Apparently, $GLOBALS['TSFE']
was not initialized here. One of the reasons may be you are in a context where it is not initialized (e.g. backend context).
Another reason may be, that you are accessing it, before it was initialized. Have a look at the description of the request workflow in the breaking change Breaking: #88540 - Changed Request Workflow for Frontend Requests.
For newer TYPO3 versions, it is no longer recommended to access $GLOBALS['TSFE']
directly. You can get the current page id like this:
In the Frontend context:
$pageArguments = $request->getAttribute('routing');
$pageId = $pageArguments->getPageId();
Documentation:
Upvotes: 2
Reputation: 6460
As suggested in the initial comments $GLOBALS['TSFE']->id
is still correct and the way to go in all current TYPO3 versions.
Upvotes: 2
Reputation: 3710
TYPO3 Version 11.5, 2021.
Only this works for me:
$contentObj = $this->configurationManager->getContentObject();
$pageId = $contentObj->data['pid'];
Notice that this only works if the current data
is a tt_content
record. This is true if your code is executed in the context of a content plugin. But if not, then you cannot rely on this.
Source: https://www.horst-muerdter.com/de/2017/08/24/typo3-8-page-id-in-controller-ermitteln/
Upvotes: 1
Reputation: 19
Try this.
Current page:
$pageUid = (int)GeneralUtility::_GET('id');
Get root page:
protected function getRootPage($pageUid) {
$page = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(RootlineUtility::class, $pageUid);
$rootLines = $page->get();
if(!empty($rootLines)) {
foreach ($rootLines as $rootLine) {
if(!empty($rootLine['is_siteroot']) && $rootLine['is_siteroot']) {
return $rootLine['uid'];
}
}
}
return 0;
}
Upvotes: 1