requiresPageContext($request)) { return $handler->handle($request); } $backendUser = $request->getAttribute('backend.user', $GLOBALS['BE_USER']); // Only process if user is authenticated in the backend if (!($backendUser instanceof BackendUserAuthentication) || !$backendUser->user) { return $handler->handle($request); } $pageId = $this->determinePageId($request); try { $request = $request->withAttribute( 'pageContext', $this->pageContextFactory->createFromRequest($request, $pageId, $backendUser) ); } catch (\Exception $e) { // If PageContext creation fails, log the error and continue without it. // Controllers can fall back to manual creation if needed. $this->logger->warning( 'Failed to create PageContext in middleware for page {page}: {error}', [ 'page' => $pageId, 'error' => $e->getMessage(), 'exception' => $e, ] ); } return $handler->handle($request); } private function determinePageId(ServerRequestInterface $request): int { $id = $request->getParsedBody()['id'] ?? $request->getQueryParams()['id'] ?? null; if ($id !== null) { return (int)$id; } $editStatement = $request->getParsedBody()['edit'] ?? $request->getQueryParams()['edit'] ?? null; if (is_array($editStatement)) { $table = key($editStatement); $uidAndAction = current($editStatement); $uid = (int)key($uidAndAction); $action = current($uidAndAction); if ($action === 'edit') { return $this->getPageIdByRecord($table, $uid); } if ($action === 'new') { return $this->getPageIdByRecord($table, $uid, true); } } $commandStatement = $request->getParsedBody()['cmd'] ?? $request->getQueryParams()['cmd'] ?? null; if (is_array($commandStatement)) { $table = key($commandStatement); $uidActionAndTarget = current($commandStatement); $uid = (int)key($uidActionAndTarget); $actionAndTarget = current($uidActionAndTarget); $action = key($actionAndTarget); $target = current($actionAndTarget); if ($action === 'delete') { return $this->getPageIdByRecord($table, $uid); } if ($action === 'copy' || $action === 'move') { return $this->getPageIdByRecord($table, (int)($target['target'] ?? $target), true); } } return 0; } private function getPageIdByRecord(string $table, int $id, bool $ignoreTable = false): int { $pageId = 0; if ($table && $id) { if (($ignoreTable || $table === 'pages') && $id >= 0) { $pageId = $id; } else { $record = BackendUtility::getRecordWSOL($table, abs($id), '*', '', false); $pageId = (int)($record['pid'] ?? 0); } } return $pageId; } /** * Check if the current request requires PageContext initialization. * * PageContext is required when: * 1. Module uses the page tree navigation component * 2. Route explicitly has 'requestPageContext' option set to true */ private function requiresPageContext(ServerRequestInterface $request): bool { return ((($module = $request->getAttribute('module')) instanceof ModuleInterface) && $module->getNavigationComponent() === '@typo3/backend/tree/page-tree-element') || ((($route = $request->getAttribute('route')) instanceof Route) && $route->getOption('requestPageContext')); } }