Sonny
Sonny

Reputation: 8336

How do I set id prefixes used by Zend_View_Helper_Navigation_XXX

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):

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

Upvotes: 1

Views: 202

Answers (1)

Phil
Phil

Reputation: 164901

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.

Upvotes: 1

Related Questions