Hi, I need to add specific tags to the HTML of the navigation menu, so I am writing a custom menu template that replaces the standard call to $site->publicNav()->menu()->renderMenu(...).
In my template, I can iterate through the menu pages:
$container = $site->publicNav()->menu()->getContainer();
$iter = new RecursiveIteratorIterator($container, RecursiveIteratorIterator::SELF_FIRST);
foreach ($iter as $page) {
// render menu item
}
In my site, I have pages marked as private that I do not want to appear in the public menu. In the loop, I need to filter the private pages but I could not find a “clean” way to do that.
I noticed that private pages->getLabel() return “[Missing page]” (or its translation), a value which seems to be set in the function getLabel declared in /application/src/Site/Navigation/Link/Page.php.
Is there another way than comparing the $page->getLabel() to the missing page label like in the following?
$translate = $this->plugin('translate');
$missingPageLabel = $this->translate('[Missing Page]');
$container = $site->publicNav()->menu()->getContainer();
$iter = new RecursiveIteratorIterator($container, RecursiveIteratorIterator::SELF_FIRST);
foreach ($iter as $page) {
$label = $page->getLabel();
if ($label !== $missingPageLabel) {
// render menu item
}
}