Feature: #93789 - Access the page context of backend requests
See forge#93789
Description
Backend modules and routes working with a page can now rely on the
request attribute page, which carries a
\TYPO3\ object. Before, every
extension had to find out on its own which page a backend request is
about.
The attribute is added to the request of
- every backend module using the page tree navigation component
(
@typo3/), either directly or inherited from its parent module, for example most of the modules below Content, andbackend/ tree/ page- tree- element - every backend route with the option
requestset toPage Context true, for example the record editing form.
The page is determined from the id argument of the request. Without
it, the page is taken from an edit statement (the page of the edited
record, or the target of a new record) or from a cmd statement (the
page of a deleted record, or the target of a copy or move command).
When no page can be determined, the context is created for page 0.
The attribute is not set when no backend user is logged in, or when
the context cannot be created, so check it before using it.
The
Page object provides
page: the requested page ID, even if the user has no access,Id page: the page record, orRecord nullif the user has no access,site: the site of the page,page: theAncestry \TYPO3\of the page in the workspace of the user, orCMS\ Core\ Domain\ Page Ancestry nullif the user has no access,page: the page TSconfig of the page,Ts Config page: the permissions of the user on the page,Permissions selectedandLanguage Ids language: the languages selected in the module and the translations of the page, asInformation \TYPO3\,CMS\ Backend\ Domain\ Model\ Language\ Page Language Information
as well as the methods
is,
get,
has,
is,
is,
get and
get.
Page,
Page,
Language and
Language are now public API. Their
constructors remain internal: the objects are created by TYPO3 Core.
Example
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Backend\Context\PageContext;
public function handleRequest(ServerRequestInterface $request): ResponseInterface
{
$pageContext = $request->getAttribute('pageContext');
if (!$pageContext instanceof PageContext) {
throw new \RuntimeException('Page context not available', 1794239826);
}
if (!$pageContext->isAccessible()) {
// Render a "no access" view
}
$pageId = $pageContext->pageId;
$moduleTsConfig = $pageContext->getModuleTsConfig('my_module');
// ...
}
A route that is not part of a module with the page tree requests the page context with an option:
return [
'my_record_action' => [
'path' => '/my-extension/record-action',
'target' => \MyVendor\MyExtension\Controller\RecordActionController::class . '::handleRequest',
'options' => [
'requestPageContext' => true,
],
],
];
Impact
Extensions can read the current page, its site, its rootline, its page
TSconfig and the permissions of the user from the page
request attribute, instead of evaluating the id argument and
fetching this information themselves.