Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Set custom class name for active li element #5650

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
93 changes: 72 additions & 21 deletions library/Zend/View/Helper/Navigation/Menu.php
Expand Up @@ -62,6 +62,13 @@ class Menu extends AbstractHelper
*/
protected $ulClass = 'navigation';

/**
* CSS class to use for the active li element
*
* @var string
*/
protected $liActiveClass = 'active';

/**
* View helper entry point:
* Retrieves helper and optionally sets container to operate on
Expand Down Expand Up @@ -115,6 +122,7 @@ public function render($container = null)
* @param int|null $maxDepth maximum depth
* @param bool $escapeLabels Whether or not to escape the labels
* @param bool $addClassToListItem Whether or not page class applied to <li> element
* @param string $liActiveClass CSS class for active LI
* @return string
*/
protected function renderDeepestMenu(
Expand All @@ -124,7 +132,8 @@ protected function renderDeepestMenu(
$minDepth,
$maxDepth,
$escapeLabels,
$addClassToListItem
$addClassToListItem,
$liActiveClass
) {
if (!$active = $this->findActive($container, $minDepth - 1, $maxDepth)) {
return '';
Expand Down Expand Up @@ -155,7 +164,7 @@ protected function renderDeepestMenu(
$liClasses = array();
// Is page active?
if ($subPage->isActive(true)) {
$liClasses[] = 'active';
$liClasses[] = $liActiveClass;
}
// Add CSS class from page to <li>
if ($addClassToListItem && $subPage->getClass()) {
Expand Down Expand Up @@ -205,7 +214,8 @@ public function renderMenu($container = null, array $options = array())
$options['minDepth'],
$options['maxDepth'],
$options['escapeLabels'],
$options['addClassToListItem']
$options['addClassToListItem'],
$options['liActiveClass']
);
} else {
$html = $this->renderNormalMenu($container,
Expand All @@ -215,7 +225,8 @@ public function renderMenu($container = null, array $options = array())
$options['maxDepth'],
$options['onlyActiveBranch'],
$options['escapeLabels'],
$options['addClassToListItem']
$options['addClassToListItem'],
$options['liActiveClass']
);
}

Expand All @@ -233,6 +244,7 @@ public function renderMenu($container = null, array $options = array())
* @param bool $onlyActive render only active branch?
* @param bool $escapeLabels Whether or not to escape the labels
* @param bool $addClassToListItem Whether or not page class applied to <li> element
* @param string $liActiveClass CSS class for active LI
* @return string
*/
protected function renderNormalMenu(
Expand All @@ -243,7 +255,8 @@ protected function renderNormalMenu(
$maxDepth,
$onlyActive,
$escapeLabels,
$addClassToListItem
$addClassToListItem,
$liActiveClass
) {
$html = '';

Expand Down Expand Up @@ -324,7 +337,7 @@ protected function renderNormalMenu(
$liClasses = array();
// Is page active?
if ($isActive) {
$liClasses[] = 'active';
$liClasses[] = $liActiveClass;
}
// Add CSS class from page to <li>
if ($addClassToListItem && $page->getClass()) {
Expand Down Expand Up @@ -422,29 +435,35 @@ public function renderPartial($container = null, $partial = null)
* 'minDepth' => null,
* 'maxDepth' => null,
* 'onlyActiveBranch' => true,
* 'renderParents' => false
* 'renderParents' => false,
* 'liActiveClass' => $liActiveClass
* ));
* </code>
*
* @param AbstractContainer $container [optional] container to
* render. Default is to render
* the container registered in
* the helper.
* @param string $ulClass [optional] CSS class to
* use for UL element. Default
* is to use the value from
* {@link getUlClass()}.
* @param string|int $indent [optional] indentation as
* a string or number of
* spaces. Default is to use
* the value retrieved from
* {@link getIndent()}.
* @param AbstractContainer $container [optional] container to
* render. Default is to render
* the container registered in
* the helper.
* @param string $ulClass [optional] CSS class to
* use for UL element. Default
* is to use the value from
* {@link getUlClass()}.
* @param string|int $indent [optional] indentation as
* a string or number of
* spaces. Default is to use
* the value retrieved from
* {@link getIndent()}.
* @param string $liActiveClass [optional] CSS class to
* use for UL element. Default
* is to use the value from
* {@link getUlClass()}.
* @return string
*/
public function renderSubMenu(
AbstractContainer $container = null,
$ulClass = null,
$indent = null
$indent = null,
$liActiveClass = null
) {
return $this->renderMenu($container, array(
'indent' => $indent,
Expand All @@ -455,6 +474,7 @@ public function renderSubMenu(
'renderParents' => false,
'escapeLabels' => true,
'addClassToListItem' => false,
'liActiveClass' => $liActiveClass
));
}

Expand Down Expand Up @@ -573,6 +593,12 @@ protected function normalizeOptions(array $options = array())
if (!isset($options['addClassToListItem'])) {
$options['addClassToListItem'] = $this->getAddClassToListItem();
}

if (isset($options['liActiveClass']) && $options['liActiveClass'] !== null) {
$options['liActiveClass'] = (string) $options['liActiveClass'];
} else {
$options['liActiveClass'] = $this->getLiActiveClass();
}

return $options;
}
Expand Down Expand Up @@ -719,4 +745,29 @@ public function getUlClass()
{
return $this->ulClass;
}

/**
* Sets CSS class to use for the active 'li' element when rendering
*
* @param string $liActiveClass CSS class to set
* @return Menu
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@return self

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samsonasik
At this point the "CS police" is not needed! Because the entire implementation from sopinorg is bad.

*/
public function setLiActiveClass($liActiveClass)
{
if (is_string($liActiveClass)) {
$this->liActiveClass = $liActiveClass;
}

return $this;
}

/**
* Returns CSS class to use for the active 'li' element when rendering
*
* @return string
*/
public function getLiActiveClass()
{
return $this->liActiveClass;
}
}
7 changes: 7 additions & 0 deletions tests/ZendTest/View/Helper/Navigation/MenuTest.php
Expand Up @@ -169,6 +169,13 @@ public function testSetUlCssClass()
$expected = $this->_getExpected('menu/css.html');
$this->assertEquals($expected, $this->_helper->render($this->_nav2));
}

public function testSetLiActiveCssClass()
{
$this->_helper->setLiActiveClass('activated');
$expected = $this->_getExpected('menu/css2.html');
$this->assertEquals($expected, $this->_helper->render($this->_nav2));
}

public function testOptionEscapeLabelsAsTrue()
{
Expand Down
@@ -0,0 +1,11 @@
<ul class="navigation">
<li>
<a href="site1">Site 1</a>
</li>
<li class="activated">
<a href="site2">Site 2</a>
</li>
<li>
<a href="site3">Site 3</a>
</li>
</ul>