vendor/kunstmaan/pagepart-bundle/Twig/Extension/PagePartTwigExtension.php line 66

Open in your IDE?
  1. <?php
  2. namespace Kunstmaan\PagePartBundle\Twig\Extension;
  3. use Doctrine\ORM\EntityManager;
  4. use Kunstmaan\PagePartBundle\Entity\PagePartRef;
  5. use Kunstmaan\PagePartBundle\Helper\HasPagePartsInterface;
  6. use Kunstmaan\PagePartBundle\Helper\PagePartInterface;
  7. use Kunstmaan\PagePartBundle\Repository\PagePartRefRepository;
  8. use Twig\Environment;
  9. use Twig\Extension\AbstractExtension;
  10. use Twig\TwigFunction;
  11. /**
  12.  * PagePartTwigExtension
  13.  *
  14.  * @final since 5.4
  15.  */
  16. class PagePartTwigExtension extends AbstractExtension
  17. {
  18.     /**
  19.      * @var EntityManager
  20.      */
  21.     protected $em;
  22.     /**
  23.      * @param EntityManager $em
  24.      */
  25.     public function __construct(EntityManager $em)
  26.     {
  27.         $this->em $em;
  28.     }
  29.     /**
  30.      * @return array
  31.      */
  32.     public function getFunctions()
  33.     {
  34.         return array(
  35.             new TwigFunction('render_pageparts', array($this'renderPageParts'), array('needs_environment' => true'needs_context' => true'is_safe' => array('html'))),
  36.             new TwigFunction('getpageparts', array('needs_environment' => true$this'getPageParts')),
  37.             new TwigFunction('has_page_parts', [$this'hasPageParts']),
  38.         );
  39.     }
  40.     /**
  41.      * @param Environment           $env
  42.      * @param array                 $twigContext The twig context
  43.      * @param HasPagePartsInterface $page        The page
  44.      * @param string                $contextName The pagepart context
  45.      * @param array                 $parameters  Some extra parameters
  46.      *
  47.      * @return string
  48.      */
  49.     public function renderPageParts(Environment $env, array $twigContextHasPagePartsInterface $page$contextName 'main', array $parameters = array())
  50.     {
  51.         $template $env->load('@KunstmaanPagePart/PagePartTwigExtension/widget.html.twig');
  52.         /* @var $entityRepository PagePartRefRepository */
  53.         $pageparts $this->getPageParts($page$contextName);
  54.         $newTwigContext array_merge($parameters, array(
  55.             'pageparts' => $pageparts,
  56.             'page' => $page,
  57.         ));
  58.         $newTwigContext array_merge($newTwigContext$twigContext);
  59.         return $template->render($newTwigContext);
  60.     }
  61.     /**
  62.      * @param HasPagePartsInterface $page    The page
  63.      * @param string                $context The pagepart context
  64.      *
  65.      * @return PagePartInterface[]
  66.      */
  67.     public function getPageParts(HasPagePartsInterface $page$context 'main')
  68.     {
  69.         /** @var $entityRepository PagePartRefRepository */
  70.         $entityRepository $this->em->getRepository('KunstmaanPagePartBundle:PagePartRef');
  71.         return $entityRepository->getPageParts($page$context);
  72.     }
  73.     /**
  74.      * @param HasPagePartsInterface $page
  75.      * @param string                $context
  76.      *
  77.      * @return bool
  78.      */
  79.     public function hasPageParts(HasPagePartsInterface $page$context 'main')
  80.     {
  81.         return $this->em->getRepository(PagePartRef::class)->hasPageParts($page$context);
  82.     }
  83. }