Issues

ZF-7212: ulId to be added similar with ulClass

Description

I think we should add an ulId param, not just an ulClass.

In certain situations there is more likely to have the ul identified by ID.

Comments

I completely agree with you on this. I need it right now.

I agree totaly. The class is declared in Zend/View/Helper/Navigation/Menu.php in line 425.

I agree as well.

Some situations where an id would be desired: * in many cases it's more efficient to write css selectors based on id * certain js menu's out there rely on id

I would expect setting an id on a navigation container would be part of the most basic features.

The patch should include the following points:


// start new ul tag
if ($ulClass && $depth ==  0) {
    $ulClass = ' class="' . $ulClass . '"';
} else {
    $ulClass = '';
}
$html .= $myIndent . '
' . self::EOL;

$attribs = array();

// start new ul tag
if (0 == $depth) {
    $attribs = array(
        'class' => $ulClass,
        'id'    => $ulId,
    );
}

$html .= $myIndent . '
_htmlAttribs($attribs) . '>' . self::EOL;

Zend_View_Helper_HtmlElement::_htmlAttribs() uses the method "_normalizeId"!


protected function _normalizeId($value)
{
    $prefix = get_class($this);
    $prefix = strtolower(trim(substr($prefix, strrpos($prefix, '_')), '_'));

    return $prefix . '-' . $value;
}

protected function _normalizeId($value)
{        
    // ... no hard coded prefix (@see ZF-10409)

    return parent::_normalizeId($value);
}

See also ZF-10409

Hi,

it's easy to use your own view helper which adds an id for example.

just create your own view helper class which extends Zend_View_Helper_Navigation_HelperAbstract or Zend_View_Helper_Navigation_Menu and overwrite or replace the mentioned parts.

use your own view helper as follows:

// in your view- or layout script <?php echo $this->navigation()->findHelper('Navigation_MyOwnMenu')->myOwnMenu()->mySetIdMethod('mymenuid')->setUlClass('fancyclass') ; ?>

dont forget to add your library in application.ini for example, so it finds your own view helper.

The patch fixes ZF-7212 and the subtask ZF-10409, because the new function for setting an ID uses the normalization of the ID attribute.

I can't get patch to apply to trunk.

Hi Rob, I will look into this and create a new patch.

New patch and more unit tests added.

Looks good to me...patches apply cleanly and all Navigation and View_Helper tests pass. As this changes the ID normalization, is there a test in place to ensure that the default normalization result is still the same as it was previously?

Hi Adam, yes: {{testSetPrefixForIdWithNull()}}.


protected function _normalizeId($value)
{
    $prefix = get_class($this);
    $prefix = strtolower(trim(substr($prefix, strrpos($prefix, '_')), '_'));

    return $prefix . '-' . $value;
}

protected $_prefixForId = null;

protected $_skipPrefixForId = false;

public function getPrefixForId()
{
    if (null === $this->_prefixForId) {
        $prefix             = get_class($this);
        $this->_prefixForId = strtolower(
            trim(substr($prefix, strrpos($prefix, '_')), '_')
        ) . '-';
    }

    return $this->_prefixForId;
}

protected function _normalizeId($value)
{        
    if (false === $this->_skipPrefixForId) {
        $prefix = $this->getPrefixForId();
 
        if (strlen($prefix)) {
            return $prefix . $value;
        }
    }
    
    return parent::_normalizeId($value);
}

I can't get patch to apply cleaning. Adam, if you can, feel free to apply it.

Fixed in trunk (1.12.0): r24962