vendor/pimcore/pimcore/bundles/CoreBundle/EventListener/Frontend/DocumentMetaDataListener.php line 70

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\DocumentResolver as DocumentResolverService;
  17. use Pimcore\Http\Request\Resolver\PimcoreContextResolver;
  18. use Pimcore\Model\Document\Page;
  19. use Pimcore\Templating\Helper\HeadMeta;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  22. use Symfony\Component\HttpKernel\KernelEvents;
  23. /**
  24.  * Adds Meta Data entries of document to HeadMeta view helper
  25.  */
  26. class DocumentMetaDataListener implements EventSubscriberInterface
  27. {
  28.     use PimcoreContextAwareTrait;
  29.     const FORCE_INJECTION '_pimcore_force_document_meta_data_injection';
  30.     /**
  31.      * @var DocumentResolverService
  32.      */
  33.     protected $documentResolverService;
  34.     /**
  35.      * @var HeadMeta
  36.      */
  37.     protected $headMeta;
  38.     /**
  39.      * @param DocumentResolverService $documentResolverService
  40.      * @param HeadMeta $headMeta
  41.      */
  42.     public function __construct(DocumentResolverService $documentResolverServiceHeadMeta $headMeta)
  43.     {
  44.         $this->documentResolverService $documentResolverService;
  45.         $this->headMeta $headMeta;
  46.     }
  47.     /**
  48.      * {@inheritdoc}
  49.      */
  50.     public static function getSubscribedEvents()
  51.     {
  52.         return [
  53.             KernelEvents::REQUEST => ['onKernelRequest'],
  54.         ];
  55.     }
  56.     /**
  57.      * Finds the nearest document for the current request if the routing/document router didn't (e.g. static routes)
  58.      *
  59.      * @param GetResponseEvent $event
  60.      */
  61.     public function onKernelRequest(GetResponseEvent $event)
  62.     {
  63.         $request $event->getRequest();
  64.         // just add meta data on master request
  65.         if (!$event->isMasterRequest() && !$event->getRequest()->attributes->get(self::FORCE_INJECTION)) {
  66.             return;
  67.         }
  68.         if (!$this->matchesPimcoreContext($requestPimcoreContextResolver::CONTEXT_DEFAULT)) {
  69.             return;
  70.         }
  71.         $document $this->documentResolverService->getDocument($request);
  72.         //check if document is set and if route is a document route for exactly that document
  73.         if ($document && $request->get('_route') == 'document_' $document->getId()) {
  74.             if ($document instanceof Page && is_array($document->getMetaData())) {
  75.                 foreach ($document->getMetaData() as $meta) {
  76.                     $this->headMeta->addRaw($meta);
  77.                 }
  78.             }
  79.         }
  80.     }
  81. }