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

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 Commercial License (PCL)
  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 PCL
  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\ViewEvent;
  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.  * @internal
  27.  */
  28. class ContentTemplateListener implements EventSubscriberInterface
  29. {
  30.     use PimcoreContextAwareTrait;
  31.     /**
  32.      * @param TemplateResolver $templateResolver
  33.      */
  34.     public function __construct(protected TemplateResolver $templateResolver)
  35.     {
  36.     }
  37.     /**
  38.      * {@inheritdoc}
  39.      */
  40.     public static function getSubscribedEvents()
  41.     {
  42.         return [
  43.             KernelEvents::VIEW => ['onKernelView'16],
  44.         ];
  45.     }
  46.     /**
  47.      * If there's a contentTemplate attribute set on the request, it was read from the document template setting from
  48.      * the router or from the sub-action renderer and takes precedence over the auto-resolved and manually configured
  49.      * template.
  50.      *
  51.      * @param ViewEvent $event
  52.      */
  53.     public function onKernelView(ViewEvent $event)
  54.     {
  55.         $request $event->getRequest();
  56.         if (!$this->matchesPimcoreContext($requestPimcoreContextResolver::CONTEXT_DEFAULT)) {
  57.             return;
  58.         }
  59.         $template $request->attributes->get('_template');
  60.         // no @Template present -> nothing to do
  61.         if (null === $template || !($template instanceof Template)) {
  62.             return;
  63.         }
  64.         $resolvedTemplate $this->templateResolver->getTemplate($request);
  65.         if (null === $resolvedTemplate) {
  66.             // no contentTemplate on the request -> nothing to do
  67.             return;
  68.         }
  69.         $template->setTemplate($resolvedTemplate);
  70.     }
  71. }