Skip to content

Commit

Permalink
Add global page model for fragments (replaces contao#1935)
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Nov 19, 2020
1 parent dc5a95d commit 03e7c41
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
16 changes: 16 additions & 0 deletions core-bundle/src/Controller/AbstractFragmentController.php
Expand Up @@ -15,6 +15,7 @@
use Contao\CoreBundle\Fragment\FragmentOptionsAwareInterface;
use Contao\FrontendTemplate;
use Contao\Model;
use Contao\PageModel;
use Contao\StringUtil;
use Contao\Template;
use Symfony\Component\DependencyInjection\Container;
Expand All @@ -26,11 +27,26 @@ abstract class AbstractFragmentController extends AbstractController implements
*/
protected $options = [];

/**
* @var PageModel|null
*/
private $pageModel;

public function setFragmentOptions(array $options): void
{
$this->options = $options;
}

protected function setPageModel(?PageModel $pageModel): void
{
$this->pageModel = $pageModel;
}

protected function getPageModel(): ?PageModel
{
return $this->pageModel;
}

/**
* Creates a template by name or from the "customTpl" field of the model.
*/
Expand Down
Expand Up @@ -14,14 +14,17 @@

use Contao\ContentModel;
use Contao\CoreBundle\Controller\AbstractFragmentController;
use Contao\PageModel;
use Contao\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

abstract class AbstractContentElementController extends AbstractFragmentController
{
public function __invoke(Request $request, ContentModel $model, string $section, array $classes = null): Response
public function __invoke(Request $request, ContentModel $model, string $section, array $classes = null, PageModel $pageModel = null): Response
{
$this->setPageModel($pageModel);

$type = $this->getType();
$template = $this->createTemplate($model, 'ce_'.$type);

Expand Down
Expand Up @@ -16,19 +16,22 @@
use Contao\CoreBundle\Controller\AbstractFragmentController;
use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\ModuleModel;
use Contao\PageModel;
use Contao\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Translation\TranslatorInterface;

abstract class AbstractFrontendModuleController extends AbstractFragmentController
{
public function __invoke(Request $request, ModuleModel $model, string $section, array $classes = null): Response
public function __invoke(Request $request, ModuleModel $model, string $section, array $classes = null, PageModel $pageModel = null): Response
{
if ($this->get('contao.routing.scope_matcher')->isBackendRequest($request)) {
return $this->getBackendWildcard($model);
}

$this->setPageModel($pageModel);

$type = $this->getType();
$template = $this->createTemplate($model, 'mod_'.$type);

Expand Down
10 changes: 10 additions & 0 deletions core-bundle/src/HttpKernel/ModelArgumentResolver.php
Expand Up @@ -15,6 +15,7 @@
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\Model;
use Contao\PageModel;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
Expand Down Expand Up @@ -79,6 +80,15 @@ private function fetchModel(Request $request, ArgumentMetadata $argument): ?Mode
return $value;
}

// Special handling for pageModel that could be globally registered
if (
isset($GLOBALS['objPage'])
&& $GLOBALS['objPage'] instanceof PageModel
&& $GLOBALS['objPage']->id === (int) $value
) {
return $GLOBALS['objPage'];
}

/** @var Model $model */
$model = $this->framework->getAdapter($argument->getType());

Expand Down

0 comments on commit 03e7c41

Please sign in to comment.