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

Commit

Permalink
Merge pull request zendframework/zendframework#5650 from sopinorg/pat…
Browse files Browse the repository at this point in the history
…ch-1

Set custom class name for active li element
  • Loading branch information
weierophinney committed Mar 4, 2014
167 parents 74576da + 7b4d532 + f3d9b3a + f7ab865 + d9d9b78 + fefc773 + 83cbc8a + 1b9fd60 + 8a8b989 + 10da323 + e6aba5a + b71d0a2 + 2aa926c + 8405746 + bf1a8de + f16339d + ed1dbc8 + dbf56da + bdb277e + 364ced3 + 9fccdf3 + 40f5963 + 9d7ddea + e1e4aa8 + 870703b + 958decf + e49a08e + a051e7e + b9f6e35 + 2bbef72 + aea30ce + faec0a2 + 098e166 + 51d6372 + 8757ace + 42477b6 + 32cf329 + 0ebadc5 + 188fa4d + a41bbf0 + a29bbbc + 941280d + d691226 + e547023 + d91c234 + 006ef75 + 69d1518 + 7dc0d1a + a995715 + e780008 + e810876 + 9e4090a + 269bf01 + 3a1f2fd + 0cab32d + 704cc7f + e20c9e6 + a0dfc1a + 76b3e1c + fd429e8 + 187ea79 + 2b43ea6 + e3b1be1 + 19f0ef6 + 33bf9c0 + 3729984 + a5ce396 + 698dbe0 + 0efca0b + 08b2a78 + ddaa846 + 7fe4493 + 0da7331 + 7226b1d + 851904c + a2eb795 + f658a03 + de83270 + 806df8a + 4e7ff23 + 409b768 + 1b97191 + d2649e3 + f0162d1 + 6f01416 + a2b3753 + 1786961 + d157fcb + 4444c37 + 192d20c + 811122b + 3a2cf9b + eb2029b + 7a6edab + 8d8a05d + c1ddf21 + 0090b4d + ef80e35 + 59b30de + 4656098 + 377b920 + ccba82c + 5d2770e + 41714a1 + 8adef43 + a78628f + b35fa7a + 3953c79 + aa28e42 + 38f9a49 + 47ed633 + 0a6bf6e + b61d89d + a1fbb6f + 238512b + f40a328 + 62dc143 + 328df3b + e12fe2d + e34a942 + 04e956d + a19a8c2 + ac4b6c0 + d28282e + bd5af3d + b976cb5 + 4feb67b + 7164be7 + 40174df + e4918ae + 1d9f9a2 + c77ae57 + 223b881 + c2f4e25 + e08b166 + e58e548 + 85e6bbd + 0ac2052 + c7ba6af + f443c57 + 1380626 + 5316b6e + a6136d4 + d233be3 + c100a2a + c6a0e2c + e7eef3c + 812f35d + 604cdcd + 5472285 + 6e2f420 + 76c4d1c + c60cca9 + 77f718e + 6bc8c90 + 6ec1d0f + 53d9070 + a67cdeb + 5d412ee + 132380c + 2809ef0 + 45fb937 + 1514822 + a0d4f17 + b620dfd + e2b5af2 + c17d4e4 commit 24db2d7
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 21 deletions.
93 changes: 72 additions & 21 deletions src/Helper/Navigation/Menu.php
Original file line number Diff line number Diff line change
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 @@ -423,29 +436,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 @@ -456,6 +475,7 @@ public function renderSubMenu(
'renderParents' => false,
'escapeLabels' => true,
'addClassToListItem' => false,
'liActiveClass' => $liActiveClass
));
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
7 changes: 7 additions & 0 deletions test/Helper/Navigation/MenuTest.php
Original file line number Diff line number Diff line change
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
11 changes: 11 additions & 0 deletions test/Helper/Navigation/_files/expected/menu/css2.html
Original file line number Diff line number Diff line change
@@ -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>

0 comments on commit 24db2d7

Please sign in to comment.