vendor/ezsystems/repository-forms/lib/EventListener/ViewTemplatesListener.php line 60

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. namespace EzSystems\RepositoryForms\EventListener;
  7. use eZ\Publish\Core\MVC\Symfony\Event\PreContentViewEvent;
  8. use eZ\Publish\Core\MVC\Symfony\MVCEvents;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. /**
  11.  * Sets the templates used by the user controller.
  12.  */
  13. class ViewTemplatesListener implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * Hash of [View type FQN] => template.
  17.      * @var array
  18.      */
  19.     protected $viewTemplates;
  20.     /**
  21.      * @var string
  22.      */
  23.     protected $pagelayout;
  24.     public static function getSubscribedEvents()
  25.     {
  26.         return [MVCEvents::PRE_CONTENT_VIEW => 'setViewTemplates'];
  27.     }
  28.     /**
  29.      * Sets the $template to use for objects of class $viewClass.
  30.      *
  31.      * @param string $viewClass FQN of a View class
  32.      * @param string $template
  33.      */
  34.     public function setViewTemplate($viewClass$template)
  35.     {
  36.         $this->viewTemplates[$viewClass] = $template;
  37.     }
  38.     /**
  39.      * Sets the pagelayout template to assign to views.
  40.      *
  41.      * @param string $pagelayout
  42.      */
  43.     public function setPagelayout($pagelayout)
  44.     {
  45.         $this->pagelayout $pagelayout;
  46.     }
  47.     /**
  48.      * If the event's view has a defined template, sets the view's template identifier,
  49.      * and the 'pagelayout' parameter.
  50.      *
  51.      * @param PreContentViewEvent $event
  52.      */
  53.     public function setViewTemplates(PreContentViewEvent $event)
  54.     {
  55.         $view $event->getContentView();
  56.         foreach ($this->viewTemplates as $viewClass => $template) {
  57.             if ($view instanceof $viewClass) {
  58.                 $view->setTemplateIdentifier($template);
  59.                 $view->addParameters(['pagelayout' => $this->pagelayout]);
  60.             }
  61.         }
  62.     }
  63. }