diff --git a/src/Helper/Navigation/Menu.php b/src/Helper/Navigation/Menu.php index 9bb04d4a..f3c10b94 100644 --- a/src/Helper/Navigation/Menu.php +++ b/src/Helper/Navigation/Menu.php @@ -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 @@ -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
  • element + * @param string $liActiveClass CSS class for active LI * @return string */ protected function renderDeepestMenu( @@ -124,7 +132,8 @@ protected function renderDeepestMenu( $minDepth, $maxDepth, $escapeLabels, - $addClassToListItem + $addClassToListItem, + $liActiveClass ) { if (!$active = $this->findActive($container, $minDepth - 1, $maxDepth)) { return ''; @@ -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
  • if ($addClassToListItem && $subPage->getClass()) { @@ -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, @@ -215,7 +225,8 @@ public function renderMenu($container = null, array $options = array()) $options['maxDepth'], $options['onlyActiveBranch'], $options['escapeLabels'], - $options['addClassToListItem'] + $options['addClassToListItem'], + $options['liActiveClass'] ); } @@ -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
  • element + * @param string $liActiveClass CSS class for active LI * @return string */ protected function renderNormalMenu( @@ -243,7 +255,8 @@ protected function renderNormalMenu( $maxDepth, $onlyActive, $escapeLabels, - $addClassToListItem + $addClassToListItem, + $liActiveClass ) { $html = ''; @@ -324,7 +337,7 @@ protected function renderNormalMenu( $liClasses = array(); // Is page active? if ($isActive) { - $liClasses[] = 'active'; + $liClasses[] = $liActiveClass; } // Add CSS class from page to
  • if ($addClassToListItem && $page->getClass()) { @@ -423,29 +436,35 @@ public function renderPartial($container = null, $partial = null) * 'minDepth' => null, * 'maxDepth' => null, * 'onlyActiveBranch' => true, - * 'renderParents' => false + * 'renderParents' => false, + * 'liActiveClass' => $liActiveClass * )); * * - * @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, @@ -456,6 +475,7 @@ public function renderSubMenu( 'renderParents' => false, 'escapeLabels' => true, 'addClassToListItem' => false, + 'liActiveClass' => $liActiveClass )); } @@ -561,6 +581,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; } @@ -707,4 +733,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 + */ + 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; + } } diff --git a/test/Helper/Navigation/MenuTest.php b/test/Helper/Navigation/MenuTest.php index 01e902c5..6bb39df6 100644 --- a/test/Helper/Navigation/MenuTest.php +++ b/test/Helper/Navigation/MenuTest.php @@ -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() { diff --git a/test/Helper/Navigation/_files/expected/menu/css2.html b/test/Helper/Navigation/_files/expected/menu/css2.html new file mode 100644 index 00000000..6a8b5bea --- /dev/null +++ b/test/Helper/Navigation/_files/expected/menu/css2.html @@ -0,0 +1,11 @@ +