Skip to content

Commit

Permalink
Simplify the pageModel getter
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Jan 29, 2024
1 parent d0d10dd commit a0245b5
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 22 deletions.
1 change: 0 additions & 1 deletion core-bundle/config/services.yaml
Expand Up @@ -1205,7 +1205,6 @@ services:
contao.twig.global.variable:
class: Contao\CoreBundle\Twig\Global\ContaoVariable
arguments:
- '@contao.framework'
- '@request_stack'
- '@contao.security.token_checker'
- '@contao.csrf.token_manager'
Expand Down
24 changes: 3 additions & 21 deletions core-bundle/src/Twig/Global/ContaoVariable.php
Expand Up @@ -5,15 +5,13 @@
namespace Contao\CoreBundle\Twig\Global;

use Contao\CoreBundle\Csrf\ContaoCsrfTokenManager;
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\CoreBundle\Security\Authentication\Token\TokenChecker;
use Contao\PageModel;
use Symfony\Component\HttpFoundation\RequestStack;

class ContaoVariable
{
public function __construct(
private readonly ContaoFramework $framework,
private readonly RequestStack $requestStack,
private readonly TokenChecker $tokenChecker,
private readonly ContaoCsrfTokenManager $tokenManager,
Expand All @@ -22,29 +20,13 @@ public function __construct(

public function getPage(): PageModel|null
{
$request = $this->requestStack->getCurrentRequest();

if (!$request || !$request->attributes->has('pageModel')) {
return null;
}

$pageModel = $request->attributes->get('pageModel');
$pageModel = $this->requestStack->getCurrentRequest()?->attributes->get('pageModel');

if ($pageModel instanceof PageModel) {
return $pageModel;
}

if (
isset($GLOBALS['objPage'])
&& $GLOBALS['objPage'] instanceof PageModel
&& (int) $GLOBALS['objPage']->id === (int) $pageModel
) {
return $GLOBALS['objPage'];
return null;
}

$this->framework->initialize();

return $this->framework->getAdapter(PageModel::class)->findByPk((int) $pageModel);
return $pageModel;
}

public function getHas_backend_user(): bool
Expand Down

0 comments on commit a0245b5

Please sign in to comment.