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

Commit

Permalink
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 30 deletions.
130 changes: 100 additions & 30 deletions src/Helper/Navigation/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
*/
class Menu extends AbstractHelper
{
/**
* Whether page class should be applied to <li> element
*
* @var bool
*/
protected $addClassToListItem = false;

/**
* Whether labels should be escaped
*
Expand Down Expand Up @@ -101,12 +108,13 @@ public function render($container = null)
* Renders the deepest active menu within [$minDepth, $maxDepth], (called
* from {@link renderMenu()})
*
* @param AbstractContainer $container container to render
* @param string $ulClass CSS class for first UL
* @param string $indent initial indentation
* @param int|null $minDepth minimum depth
* @param int|null $maxDepth maximum depth
* @param bool $escapeLabels Whether or not to escape the labels
* @param AbstractContainer $container container to render
* @param string $ulClass CSS class for first UL
* @param string $indent initial indentation
* @param int|null $minDepth minimum depth
* @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
* @return string
*/
protected function renderDeepestMenu(
Expand All @@ -115,7 +123,8 @@ protected function renderDeepestMenu(
$indent,
$minDepth,
$maxDepth,
$escapeLabels
$escapeLabels,
$addClassToListItem
) {
if (!$active = $this->findActive($container, $minDepth - 1, $maxDepth)) {
return '';
Expand All @@ -141,9 +150,21 @@ protected function renderDeepestMenu(
if (!$this->accept($subPage)) {
continue;
}
$liClass = $subPage->isActive(true) ? ' class="active"' : '';

// render li tag and page
$liClasses = array();
// Is page active?
if ($subPage->isActive(true)) {
$liClasses[] = 'active';
}
// Add CSS class from page to <li>
if ($addClassToListItem && $subPage->getClass()) {
$liClasses[] = $subPage->getClass();
}
$liClass = empty($liClasses) ? '' : ' class="' . implode(' ', $liClasses) . '"';

$html .= $indent . ' <li' . $liClass . '>' . self::EOL;
$html .= $indent . ' ' . $this->htmlify($subPage, $escapeLabels) . self::EOL;
$html .= $indent . ' ' . $this->htmlify($subPage, $escapeLabels, $addClassToListItem) . self::EOL;
$html .= $indent . ' </li>' . self::EOL;
}

Expand Down Expand Up @@ -183,15 +204,19 @@ public function renderMenu($container = null, array $options = array())
$options['indent'],
$options['minDepth'],
$options['maxDepth'],
$options['escapeLabels']);
$options['escapeLabels'],
$options['addClassToListItem']
);
} else {
$html = $this->renderNormalMenu($container,
$options['ulClass'],
$options['indent'],
$options['minDepth'],
$options['maxDepth'],
$options['onlyActiveBranch'],
$options['escapeLabels']);
$options['escapeLabels'],
$options['addClassToListItem']
);
}

return $html;
Expand All @@ -200,13 +225,14 @@ public function renderMenu($container = null, array $options = array())
/**
* Renders a normal menu (called from {@link renderMenu()})
*
* @param AbstractContainer $container container to render
* @param string $ulClass CSS class for first UL
* @param string $indent initial indentation
* @param int|null $minDepth minimum depth
* @param int|null $maxDepth maximum depth
* @param bool $onlyActive render only active branch?
* @param bool $escapeLabels Whether or not to escape the labels
* @param AbstractContainer $container container to render
* @param string $ulClass CSS class for first UL
* @param string $indent initial indentation
* @param int|null $minDepth minimum depth
* @param int|null $maxDepth maximum depth
* @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
* @return string
*/
protected function renderNormalMenu(
Expand All @@ -216,7 +242,8 @@ protected function renderNormalMenu(
$minDepth,
$maxDepth,
$onlyActive,
$escapeLabels
$escapeLabels,
$addClassToListItem
) {
$html = '';

Expand Down Expand Up @@ -294,9 +321,19 @@ protected function renderNormalMenu(
}

// render li tag and page
$liClass = $isActive ? ' class="active"' : '';
$liClasses = array();
// Is page active?
if ($isActive) {
$liClasses[] = 'active';
}
// Add CSS class from page to <li>
if ($addClassToListItem && $page->getClass()) {
$liClasses[] = $page->getClass();
}
$liClass = empty($liClasses) ? '' : ' class="' . implode(' ', $liClasses) . '"';

$html .= $myIndent . ' <li' . $liClass . '>' . self::EOL
. $myIndent . ' ' . $this->htmlify($page, $escapeLabels) . self::EOL;
. $myIndent . ' ' . $this->htmlify($page, $escapeLabels, $addClassToListItem) . self::EOL;

// store as previous depth for next iteration
$prevDepth = $depth;
Expand Down Expand Up @@ -410,13 +447,14 @@ public function renderSubMenu(
$indent = null
) {
return $this->renderMenu($container, array(
'indent' => $indent,
'ulClass' => $ulClass,
'minDepth' => null,
'maxDepth' => null,
'onlyActiveBranch' => true,
'renderParents' => false,
'escapeLabels' => true
'indent' => $indent,
'ulClass' => $ulClass,
'minDepth' => null,
'maxDepth' => null,
'onlyActiveBranch' => true,
'renderParents' => false,
'escapeLabels' => true,
'addClassToListItem' => false,
));
}

Expand All @@ -430,7 +468,7 @@ public function renderSubMenu(
* @param bool $escapeLabel Whether or not to escape the label
* @return string
*/
public function htmlify(AbstractPage $page, $escapeLabel = true)
public function htmlify(AbstractPage $page, $escapeLabel = true, $addClassToListItem = false)
{
// get label and title for translating
$label = $page->getLabel();
Expand All @@ -451,9 +489,12 @@ public function htmlify(AbstractPage $page, $escapeLabel = true)
$attribs = array(
'id' => $page->getId(),
'title' => $title,
'class' => $page->getClass()
);

if ($addClassToListItem === false) {
$attribs['class'] = $page->getClass();
}

// does page have a href?
$href = $page->getHref();
if ($href) {
Expand Down Expand Up @@ -528,6 +569,10 @@ protected function normalizeOptions(array $options = array())
$options['renderParents'] = $this->getRenderParents();
}

if (!isset($options['addClassToListItem'])) {
$options['addClassToListItem'] = $this->getAddClassToListItem();
}

return $options;
}

Expand All @@ -543,6 +588,31 @@ public function escapeLabels($flag = true)
return $this;
}

/**
* Enables/disables page class applied to <li> element
*
* @param bool $flag [optional] page class applied to <li> element
* Default is true.
* @return Menu fluent interface, returns self
*/
public function setAddClassToListItem($flag = true)
{
$this->addClassToListItem = (bool) $flag;
return $this;
}

/**
* Returns flag indicating whether page class should be applied to <li> element
*
* By default, this value is false.
*
* @return bool whether parents should be rendered
*/
public function getAddClassToListItem()
{
return $this->addClassToListItem;
}

/**
* Sets a flag indicating whether only active branch should be rendered
*
Expand Down
52 changes: 52 additions & 0 deletions test/Helper/Navigation/MenuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,58 @@ public function testOptionOnlyActiveBranchNoParentsAndBothDepthsSpecified()
$this->assertEquals($expected, $actual);
}

public function testRenderingWithoutPageClassToLi()
{
$container = new \Zend\Navigation\Navigation($this->_nav2->toArray());
$container->addPage(array(
'label' => 'Class test',
'uri' => 'test',
'class' => 'foobar',
));

$expected = $this->_getExpected('menu/addclasstolistitem_as_false.html');
$actual = $this->_helper->renderMenu($container);

$this->assertEquals($expected, $actual);
}

public function testRenderingWithPageClassToLi()
{
$options = array(
'addClassToListItem' => true,
);

$container = new \Zend\Navigation\Navigation($this->_nav2->toArray());
$container->addPage(array(
'label' => 'Class test',
'uri' => 'test',
'class' => 'foobar',
));

$expected = $this->_getExpected('menu/addclasstolistitem_as_true.html');
$actual = $this->_helper->renderMenu($container, $options);

$this->assertEquals($expected, $actual);
}

public function testRenderDeepestMenuWithPageClassToLi()
{
$options = array(
'addClassToListItem' => true,
'onlyActiveBranch' => true,
'renderParents' => false,
);

$pages = $this->_nav2->toArray();
$pages[1]['class'] = 'foobar';
$container = new \Zend\Navigation\Navigation($pages);

$expected = $this->_getExpected('menu/onlyactivebranch_addclasstolistitem.html');
$actual = $this->_helper->renderMenu($container, $options);

$this->assertEquals($expected, $actual);
}

/**
* Returns the contens of the expected $file, normalizes newlines
* @param string $file
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<ul class="navigation">
<li>
<a href="site1">Site 1</a>
</li>
<li class="active">
<a href="site2">Site 2</a>
</li>
<li>
<a href="site3">Site 3</a>
</li>
<li>
<a class="foobar" href="test">Class test</a>
</li>
</ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<ul class="navigation">
<li>
<a href="site1">Site 1</a>
</li>
<li class="active">
<a href="site2">Site 2</a>
</li>
<li>
<a href="site3">Site 3</a>
</li>
<li class="foobar">
<a href="test">Class test</a>
</li>
</ul>
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="active foobar">
<a href="site2">Site 2</a>
</li>
<li>
<a href="site3">Site 3</a>
</li>
</ul>

0 comments on commit f16339d

Please sign in to comment.