|
<?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 |
|
); |
|
} |
|
} |