Hiding private pages in custom menu template

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
  }
}

The URI for these “missing” pages gets set to null when we make the nav. You could detect that, maybe?

Thanks John, this is not really “cleaner” as my solution. Maybe it could be useful to add a method like $page->isPrivate(), don’t you think?