vendor/pimcore/pimcore/bundles/CoreBundle/EventListener/Frontend/ContentTemplateListener.php line 65

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  * @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  * @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace Pimcore\Bundle\CoreBundle\EventListener\Frontend;
  15. use Pimcore\Bundle\CoreBundle\EventListener\Traits\PimcoreContextAwareTrait;
  16. use Pimcore\Http\Request\Resolver\PimcoreContextResolver;
  17. use Pimcore\Http\Request\Resolver\TemplateResolver;
  18. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
  21. use Symfony\Component\HttpKernel\KernelEvents;
  22. /**
  23.  * If a contentTemplate attribute was set on the request (done by router when building a document route), extract the
  24.  * value and set it on the Template annotation. This handles custom template files being configured on documents.
  25.  */
  26. class ContentTemplateListener implements EventSubscriberInterface
  27. {
  28.     use PimcoreContextAwareTrait;
  29.     /**
  30.      * @var TemplateResolver
  31.      */
  32.     protected $templateResolver;
  33.     /**
  34.      * @param TemplateResolver $templateResolver
  35.      */
  36.     public function __construct(TemplateResolver $templateResolver)
  37.     {
  38.         $this->templateResolver $templateResolver;
  39.     }
  40.     /**
  41.      * @inheritDoc
  42.      */
  43.     public static function getSubscribedEvents()
  44.     {
  45.         return [
  46.             // this must run after the TemplateControllerListener set a potential template and before the TemplateListener
  47.             // renders the view
  48.             KernelEvents::VIEW => ['onKernelView'16]
  49.         ];
  50.     }
  51.     /**
  52.      * If there's a contentTemplate attribute set on the request, it was read from the document template setting from
  53.      * the router or from the sub-action renderer and takes precedence over the auto-resolved and manually configured
  54.      * template.
  55.      *
  56.      * @param GetResponseForControllerResultEvent $event
  57.      */
  58.     public function onKernelView(GetResponseForControllerResultEvent $event)
  59.     {
  60.         $request $event->getRequest();
  61.         if (!$this->matchesPimcoreContext($requestPimcoreContextResolver::CONTEXT_DEFAULT)) {
  62.             return;
  63.         }
  64.         $template $request->attributes->get('_template');
  65.         // no @Template present -> nothing to do
  66.         if (null === $template || !($template instanceof Template)) {
  67.             return;
  68.         }
  69.         $resolvedTemplate $this->templateResolver->getTemplate($request);
  70.         if (null === $resolvedTemplate) {
  71.             // no contentTemplate on the request -> nothing to do
  72.             return;
  73.         }
  74.         $template->setTemplate($resolvedTemplate);
  75.     }
  76. }