src/Twig/NavigationHelper.php line 52

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by Elements.at New Media Solutions GmbH
  4.  *
  5.  */
  6. namespace App\Twig;
  7. use Elements\Bundle\SeoHelperBundle\Service\PrettyUrl;
  8. use Pimcore\Http\Request\Resolver\DocumentResolver;
  9. use Pimcore\Model\DataObject;
  10. use Pimcore\Model\DataObject\Folder;
  11. use Pimcore\Model\Document;
  12. use Pimcore\Model\Document\Page;
  13. use Pimcore\Translation\Translator;
  14. use Pimcore\Twig\Extension\Templating\Navigation;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Symfony\Component\Routing\RouterInterface;
  17. use Twig\Extension\AbstractExtension;
  18. use Twig\TwigFunction;
  19. class NavigationHelper extends AbstractExtension
  20. {
  21.     public function __construct(
  22.         private PrettyUrl $prettyUrl,
  23.         private Navigation $navigation,
  24.         private Translator $translator,
  25.         private RouterInterface $router,
  26.         private RequestStack $requestStack,
  27.         private DocumentResolver $documentResolver,
  28.     ) {
  29.     }
  30.     public function getFunctions(): array
  31.     {
  32.         return [
  33.             new TwigFunction('metaNavigation', [$this'getMetaNavigation'], [ 'is_safe' => ['html'] ]),
  34.             new TwigFunction('mainNavigation', [$this'getMainNavigation'], [ 'is_safe' => ['html'] ]),
  35.             new TwigFunction('languageNavigation', [$this'getLanguageNavigation'], [ 'is_safe' => ['html'] ]),
  36.             new TwigFunction('getSiteForDocument', [$this'getSiteForDocument'], [ 'is_safe' => ['html'] ]),
  37.         ];
  38.     }
  39.     /**
  40.      * @param Document $activeDocument
  41.      *
  42.      * @return string
  43.      *
  44.      * @throws \Exception
  45.      */
  46.     public function getMetaNavigation(Document $activeDocument): string
  47.     {
  48.         $rootNode $activeDocument->getProperty('languageRoot') ?? Document::getById(1);
  49.         $mainNavStartNode $rootNode ?: Page::getById(1);
  50.         $mainNavigation $this->navigation->build([
  51.             'active' => $activeDocument,
  52.             'root' => $mainNavStartNode,
  53.             'pageCallback' => function ($page$document) {
  54.                 $page->setCustomSetting('exclude'$document->getProperty('navigation_exclude'));
  55.                 $page->setCustomSetting('resort'$document->getProperty('resort') ?: 'false');
  56.                 $page->setCustomSetting('document'$document);
  57.             },
  58.         ]);
  59.         $this->navigation->menu()->setTemplate('navigation/partials/navbar-meta-links.html.twig');
  60.         return $this->navigation->menu()->render($mainNavigation);
  61.     }
  62.     /**
  63.      * @param Document $activeDocument
  64.      *
  65.      * @return string
  66.      *
  67.      * @throws \Exception
  68.      */
  69.     public function getMainNavigation(Document $activeDocument): string
  70.     {
  71.         $rootNode $activeDocument->getProperty('navRoot') ?? Document::getById(1);
  72.         $mainNavStartNode $rootNode ?: Page::getById(1);
  73.         $mainNavigation $this->navigation->build([
  74.             'active' => $activeDocument,
  75.             'root' => $mainNavStartNode,
  76.             'pageCallback' => function ($page$document) {
  77.                 $page->setCustomSetting('exclude'$document->getProperty('navigation_exclude'));
  78.                 $page->setCustomSetting('bigSubnav'$document->getProperty('bigSubnav'));
  79.                 $page->setCustomSetting('hasBrand'$document->getProperty('hasBrand'));
  80.                 $page->setCustomSetting('document'$document);
  81.             },
  82.         ]);
  83.         $this->navigation->menu()->setTemplate('navigation/partials/navbar-main.html.twig');
  84.         return $this->navigation->menu()->render($mainNavigation);
  85.     }
  86.     /**
  87.      * @param Document|null $document
  88.      *
  89.      * @return mixed
  90.      *
  91.      */
  92.     public function getLanguageNavigation(Document $document null)
  93.     {
  94.         $request $this->requestStack->getCurrentRequest();
  95.         if ($request == '') {
  96.             return [];
  97.         }
  98.         if ($document == null) {
  99.             $document $this->documentResolver->getDocument();
  100.         }
  101.         $requestLanguage $request->getLocale();
  102.         $service = new \Pimcore\Model\Document\Service;
  103.         $translations $service->getTranslations($document);
  104.         if (empty($translations)) {
  105.             $translations[$request->getLocale()] = $document->getId();
  106.         }
  107.         $links = [ ];
  108.         try {
  109.             $route $this->router->match($request->getPathInfo());
  110.         } catch (\Symfony\Component\Routing\Exception\ResourceNotFoundException $e) {
  111.             $route = [];
  112.         }
  113.         $routename $route['_route'] ?? '';
  114.         $id $request->get('id''');
  115.         if ($id instanceof DataObject) {
  116.             $obj $id;
  117.             $id $id->getId();
  118.         } else {
  119.             $obj \Pimcore\Model\DataObject::getById($id);
  120.         }
  121.         foreach (\Pimcore\Tool::getValidLanguages() as $language) {
  122.             if (isset($translations[$language])) {
  123.                 $target '/' $language;
  124.                 $localizedDocument \Pimcore\Model\Document::getById($translations[$language]);
  125.                 if ($localizedDocument) {
  126.                     $target $localizedDocument->getFullPath();
  127.                     if (!$localizedDocument->isPublished()) {
  128.                         continue;
  129.                     }
  130.                 }
  131.                 if ($routename != '' && $obj != '' && $id != '') {
  132.                     if (!$obj instanceof Folder && $obj->getClass()->getLinkGenerator() != '') {
  133.                         $target $obj->getClass()->getLinkGenerator()->generate($obj, ['language' => $language'document' => $localizedDocument ?: $document'toLanguage' => $language]);
  134.                     } elseif (method_exists($obj'getName')) {
  135.                         $n $obj->getName('en');
  136.                         if ($obj->getName($language) != '') {
  137.                             $n $obj->getName($language);
  138.                         }
  139.                         if ($n != '') {
  140.                             $linkConfig = [
  141.                                 'path' => $target,
  142.                                 'name' => $n,
  143.                                 'id' => $obj->getId(),
  144.                             ];
  145.                             $target $this->prettyUrl->prettyUrl($linkConfig$routenamefalsetrue$language);
  146.                         }
  147.                     } elseif (method_exists($obj'getTitle')) {
  148.                         $n $obj->getTitle('en');
  149.                         if ($obj->getTitle($language) != '') {
  150.                             $n $obj->getTitle($language);
  151.                         }
  152.                         if ($n != '') {
  153.                             $linkConfig = [
  154.                                 'path' => $target,
  155.                                 'name' => $n,
  156.                                 'id' => $obj->getId(),
  157.                             ];
  158.                             $target $this->prettyUrl->prettyUrl($linkConfig$routenamefalsetrue$language);
  159.                         }
  160.                     } else {
  161.                         $target '';
  162.                     }
  163.                 }
  164.                 $target preg_replace("/^\/(.*)\/\//"'/'$target);
  165.                 if ($target != '') {
  166.                     $links[$target]['text'] = $this->translator->trans('global.language.' $language, [], null$requestLanguage);
  167.                     $links[$target]['lang'] = $language;
  168.                 }
  169.             }
  170.         }
  171.         return $links;
  172.     }
  173.     public function getSiteForDocument($document): ?string
  174.     {
  175.         return \Pimcore\Tool\Frontend::getSiteForDocument($document)?->getMainDomain();
  176.     }
  177. }