Skip to content

Instantly share code, notes, and snippets.

@aarongerig
Last active January 13, 2021 11:19
Show Gist options
  • Save aarongerig/0e824194d0420e44d1e7b1b9bd8fdebc to your computer and use it in GitHub Desktop.
Save aarongerig/0e824194d0420e44d1e7b1b9bd8fdebc to your computer and use it in GitHub Desktop.
[Pimcore] Custom Twig Navigation Builder
{% set navigation = app_build_nav(document, navStartNode) %}
{% set menuRenderer = pimcore_nav_renderer('menu') %}
<ul>
{% for page in navigation %}
{% if (page.isVisible and menuRenderer.accept(page)) %}
<li class="{{ page.isActive(true) ? 'active' }}">
<a href="{{ page.href }}">{{ page.label }}</a>
{% if page.hasPages %}
<ul>
{% for child in page.pages %}
{% if (child.isVisible and menuRenderer.accept(child)) %}
<li class="{{ child.isActive(true) ? 'active' }}">
<a href="{{ child.href }}">{{ child.label }}</a>
</li>
{% endif %}
{% endfor %}
</ul>
{% endif %}
</li>
{% endif %}
{% endfor %}
</ul>
<?php
declare(strict_types=1);
namespace AppBundle\Twig\Extension;
use Pimcore\Model\DataObject;
use Pimcore\Model\Document;
use Pimcore\Navigation\Container;
use Pimcore\Templating\Helper\Navigation;
use Symfony\Component\Routing\Router;
class NavigationExtension extends \Twig_Extension
{
/**
* @var Navigation
*/
private $navigationHelper;
/**
* @var RouterInterface
*/
private $router;
/**
* @param Navigation $navigationHelper
* @param RouterInterface $router
*/
public function __construct(Navigation $navigationHelper, RouterInterface $router)
{
$this->navigationHelper = $navigationHelper;
$this->router = $router;
}
/**
* {@inheritdoc}
*/
public function getFunctions(): array
{
return [
new \Twig_Function('app_build_nav', [$this, 'buildNavigation']),
];
}
/**
* @param Document $activeDocument
* @param Document|null $navigationRootDocument
* @param string|null $htmlMenuPrefix
* @param bool|string $cache
* @return Container
*/
public function buildNavigation(
Document $activeDocument,
Document $navigationRootDocument = null,
string $htmlMenuPrefix = null,
$cache = true
): Container {
return $this->navigationHelper->buildNavigation(
$activeDocument,
$navigationRootDocument,
$htmlMenuPrefix,
function($page, $document) {
/** @var $document \Pimcore\Model\Document */
/** @var $page \Pimcore\Navigation\Page\Document */
if ($document->getProperty('templateType') === 'news') {
$list = new DataObject\News\Listing();
$list->load();
foreach ($list as $news) {
$detailLink = $this->router->generate('news', [
'id' => $news->getId(),
'text' => $news->getTitle(),
'prefix' => $activeDocument->getFullPath(),
]);
$uri = new \Pimcore\Navigation\Page\Document([
'label' => $news->getTitle(),
'id' => sprintf('object-%s', $object->getId()),
'uri' => $detailLink
]);
$page->addPage($uri);
}
}
},
$cache
);
}
}
services:
AppBundle\Twig\Extension\NavigationExtension:
arguments:
- '@pimcore.templating.view_helper.navigation'
- '@router'
tags:
- { name: twig.extension }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment