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\Templating\Helper\Navigation;
  20. use Twig\Extension\AbstractExtension;
  21. use Twig\TwigFunction;
  22. class NavigationExtension extends AbstractExtension
  23. {
  24.     /**
  25.      * @var Navigation
  26.      */
  27.     private $navigationHelper;
  28.     /**
  29.      * @param Navigation $navigationHelper
  30.      */
  31.     public function __construct(Navigation $navigationHelper)
  32.     {
  33.         $this->navigationHelper $navigationHelper;
  34.     }
  35.     public function getFunctions(): array
  36.     {
  37.         return [
  38.             new TwigFunction('pimcore_build_nav', [$this'buildNavigation']),
  39.             new TwigFunction('pimcore_render_nav', [$this'render'], [
  40.                 'is_safe' => ['html'],
  41.             ]),
  42.             new TwigFunction('pimcore_nav_renderer', [$this'getRenderer']),
  43.         ];
  44.     }
  45.     /**
  46.      * This is essentially the same as the buildNavigation method on the
  47.      * Navigation helper, but without a pageCallback as it does not make
  48.      * sense in Twig context. If you need a more customized navigation
  49.      * with a callback, build the navigation externally, pass it to the
  50.      * view and just call render through the extension.
  51.      *
  52.      * @param mixed $params config array or active document (legacy mode)
  53.      * @param Document|null $navigationRootDocument
  54.      * @param string|null $htmlMenuPrefix
  55.      * @param bool|string $cache
  56.      *
  57.      * @return Container
  58.      *
  59.      * @throws \Exception
  60.      */
  61.     public function buildNavigation(
  62.         $params null,
  63.         Document $navigationRootDocument null,
  64.         string $htmlMenuPrefix null,
  65.         $cache true
  66.     ): Container {
  67.         if (is_array($params)) {
  68.             // using param configuration
  69.             return $this->navigationHelper->build($params);
  70.         } else {
  71.             @trigger_error(sprintf('Passing active document as first param to "pimcore_build_nav" Twig function or %s is deprecated and will throw exception in Pimcore 10. Pass array arguments instead.'__METHOD__), E_USER_DEPRECATED);
  72.             // using deprecated argument configuration ($params = navigation root document)
  73.             return $this->navigationHelper->buildNavigation(
  74.                 $params,
  75.                 $navigationRootDocument,
  76.                 $htmlMenuPrefix,
  77.                 null,
  78.                 $cache
  79.             );
  80.         }
  81.     }
  82.     /**
  83.      * Loads a renderer instance
  84.      *
  85.      * @param string $alias
  86.      *
  87.      * @return RendererInterface
  88.      */
  89.     public function getRenderer(string $alias): RendererInterface
  90.     {
  91.         return $this->navigationHelper->getRenderer($alias);
  92.     }
  93.     /**
  94.      * Renders a navigation with the given renderer
  95.      *
  96.      * @param Container $container
  97.      * @param string $rendererName
  98.      * @param string|null $renderMethod     Optional render method to use (e.g. menu -> renderMenu)
  99.      * @param array<int, mixed> $rendererArguments      Option arguments to pass to the render method after the container
  100.      *
  101.      * @return string
  102.      */
  103.     public function render(
  104.         Container $container,
  105.         string $rendererName 'menu',
  106.         string $renderMethod null,
  107.         ...$rendererArguments
  108.     ) {
  109.         return call_user_func_array([$this->navigationHelper'render'], func_get_args());
  110.     }
  111. }