vendor/pimcore/pimcore/lib/Twig/Extension/NavigationExtension.php line 69

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Pimcore
  5.  *
  6.  * This source file is available under two different licenses:
  7.  * - GNU General Public License version 3 (GPLv3)
  8.  * - Pimcore Commercial License (PCL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  14.  */
  15. namespace Pimcore\Twig\Extension;
  16. use Pimcore\Model\Document;
  17. use Pimcore\Navigation\Container;
  18. use Pimcore\Navigation\Renderer\RendererInterface;
  19. use Pimcore\Twig\Extension\Templating\Navigation;
  20. use Twig\Extension\AbstractExtension;
  21. use Twig\TwigFunction;
  22. /**
  23.  * @internal
  24.  */
  25. class NavigationExtension extends AbstractExtension
  26. {
  27.     /**
  28.      * @var Navigation
  29.      */
  30.     private $navigationExtension;
  31.     /**
  32.      * @param Navigation $navigationExtension
  33.      */
  34.     public function __construct(Navigation $navigationExtension)
  35.     {
  36.         $this->navigationExtension $navigationExtension;
  37.     }
  38.     public function getFunctions(): array
  39.     {
  40.         return [
  41.             new TwigFunction('pimcore_build_nav', [$this'buildNavigation']),
  42.             new TwigFunction('pimcore_render_nav', [$this'render'], [
  43.                 'is_safe' => ['html'],
  44.             ]),
  45.             new TwigFunction('pimcore_nav_renderer', [$this'getRenderer']),
  46.         ];
  47.     }
  48.     /**
  49.      * This is essentially the same as the build() method on the
  50.      * Navigation helper, but without a pageCallback as it does not make
  51.      * sense in Twig context. If you need a more customized navigation
  52.      * with a callback, build the navigation externally, pass it to the
  53.      * view and just call render through the extension.
  54.      *
  55.      * @param mixed $params config array or active document (legacy mode)
  56.      *
  57.      * @return Container
  58.      *
  59.      * @throws \Exception
  60.      */
  61.     public function buildNavigation(
  62.         $params null
  63.     ): Container {
  64.         return $this->navigationExtension->build($params);
  65.     }
  66.     /**
  67.      * Loads a renderer instance
  68.      *
  69.      * @param string $alias
  70.      *
  71.      * @return RendererInterface
  72.      */
  73.     public function getRenderer(string $alias): RendererInterface
  74.     {
  75.         return $this->navigationExtension->getRenderer($alias);
  76.     }
  77.     /**
  78.      * Renders a navigation with the given renderer
  79.      *
  80.      * @param Container $container
  81.      * @param string $rendererName
  82.      * @param string|null $renderMethod     Optional render method to use (e.g. menu -> renderMenu)
  83.      * @param array<int, mixed> $rendererArguments      Option arguments to pass to the render method after the container
  84.      *
  85.      * @return string
  86.      */
  87.     public function render(
  88.         Container $container,
  89.         string $rendererName 'menu',
  90.         string $renderMethod null,
  91.         ...$rendererArguments
  92.     ) {
  93.         return call_user_func_array([$this->navigationExtension'render'], func_get_args());
  94.     }
  95. }