1

When I render a Zend_Nagivation instance in my view, by default the anchor tags have id's assigned with the prefix of the view helper, followed by a dash, and then the page's id.

Examples of Page 1's anchor id (all using the same Zend_Nagivation instance):

  • Zend_View_Helper_Navigation_Menu = "menu-1"
  • Zend_View_Helper_Navigation_Breadcrumbs = "breadcrumbs-1"
  • My_View_Helper_Navigation_MyMenu = "mymenu-1"

This is perfect for most cases, but I'd like to set that prefix manually, and I can't find a way to do it.


Solution

Specifying the prefix can be accomplished by adding the following code, and then calling setIdPrefix() when rendering:

class My_View_Helper_Navigation_MyMenu extends Zend_View_Helper_Navigation_Menu
{
    protected $_idPrefix = null;

    /**
     * Set the id prefix to use for _normalizeId()
     *
     * @param string $prefix
     * @return My_View_Helper_Navigation_MyMenu
     */
    public function setIdPrefix($prefix)
    {
        if (is_string($prefix)) {
            $this->_idPrefix = $prefix;
        }

        return $this;
    }

    /**
     * Use the id prefix specified or proxy to the parent
     *
     * @param string $value
     * @return string
     */
    protected function _normalizeId($value)
    {
        if (is_null($this->_idPrefix)) {
            return parent::_normalizeId($value);
        } else {
            return $this->_idPrefix . '-' . $value;
        }
    }
}
Sonny
  • 8,204
  • 7
  • 63
  • 134
  • How is the navigation configured? From ini? Classes? – Iznogood Dec 08 '10 at 22:59
  • The `Zend_Navigation` instances, I currently have four, two are built from the database and two are from xml files. This behavior occurs for all of them, regardless of the data source. – Sonny Dec 08 '10 at 23:03
  • You know you can call your helper "Menu" instead of "MyMenu" in which case the plugin loader will load yours in place of the ZF one – Phil Dec 09 '10 at 22:16
  • @Phil - Thanks! I remember having naming trouble when I originally wrote the helper. I may have mistook one issue for another. Here's the thread: http://stackoverflow.com/questions/2364695/how-do-i-extend-the-zend-navigation-menu-view-helper – Sonny Dec 09 '10 at 22:56
  • 1
    Ah, forgot about the navigation helper proxy system. Overloading this would require more work. – Phil Dec 09 '10 at 23:07

1 Answers1

1

The culprit is Zend_View_Helper_Navigation_HelperAbstract::_normalizeId() for which I see no other solution than to create your own custom version of each navigation view helper you require.

Phil
  • 157,677
  • 23
  • 242
  • 245
  • 1
    I see that now Phil, thanks! It's using `get_class()` and then using the last part of the class name (underscore separated) and lower-cased to make the prefix. – Sonny Dec 09 '10 at 14:22
  • Your answer led me to the solution, so I'm accepting it. Thanks Phil! – Sonny Dec 09 '10 at 15:41