Search code examples
phpzend-frameworkzend-navigation

Zend_Navigation with hidden pages


I have my Zend_Navigation loaded from a PHP array (but that's irrelevant...) and I'm using the navigation menu helper to generate a menu based on the loaded navigation. Some menu items must not appear in the outputted menu, so I simply set "'visible' => false" in my array for that page and there you go! But if the URL of an 'hidden' menu is accessed, the findActive($container) view helper method returns an empty array, thus the page from the container is not returned, even if it should (like if the page didn't exist); leaving the browser title empty, etc.

Since both the menu navigation helper and the navigation view helper uses the 'visible' option to discard the page (through the method accept($page)), this setting is useless in my case.

What would be the best way to go from here?


Solution

  • I actually just found a much more elegant solution. Simply add the following line before your findActive() call, and it will return an invisible page, if selected:

    $this->navigation()->setRenderInvisible(true);
    

    For example, the following code:

    Zend_Debug::dump($this->navigation()
                          ->findActive($this->navigation()->getContainer()));
    $this->navigation()->setRenderInvisible(true);
    Zend_Debug::dump($this->navigation()
                          ->findActive($this->navigation()->getContainer()));
    

    Produces:

    array(0) {
    }
    array(2) {
      ["page"] => object(Zend_Navigation_Page_Mvc)#33 (24) {
        ... PAGE INFORMATION ...
      }
      ["depth"] => int(0)
    }
    

    The curious part is that it doesn't affect the rendering of the menu - i.e. hidden pages are still hidden. That doesn't make much sense though, so I'd recommend setting it to false again to make sure it doesn't cause problems in the future.