NavigationItem: Allow to dynamically decide whether to render an item

refs #5600
This commit is contained in:
Johannes Meyer 2015-09-17 09:11:17 +02:00
parent 2d9f20f2c5
commit b1ee12f721
3 changed files with 54 additions and 7 deletions

View File

@ -108,6 +108,13 @@ class NavigationItem implements IteratorAggregate
*/ */
protected $renderer; protected $renderer;
/**
* Whether to render this item
*
* @var bool
*/
protected $render;
/** /**
* Create a new NavigationItem * Create a new NavigationItem
* *
@ -117,6 +124,7 @@ class NavigationItem implements IteratorAggregate
public function __construct($name, array $properties = null) public function __construct($name, array $properties = null)
{ {
$this->setName($name); $this->setName($name);
$this->render = true;
$this->priority = 100; $this->priority = 100;
$this->children = new Navigation(); $this->children = new Navigation();
@ -689,6 +697,41 @@ class NavigationItem implements IteratorAggregate
return $this->renderer; return $this->renderer;
} }
/**
* Set whether this item should be rendered
*
* @param bool $state
*
* @return $this
*/
public function setRender($state = true)
{
$this->render = (bool) $state;
return $this;
}
/**
* Return whether this item should be rendered
*
* @return bool
*/
public function getRender()
{
return $this->render;
}
/**
* Return whether this item should be rendered
*
* Alias for NavigationItem::getRender().
*
* @return bool
*/
public function shouldRender()
{
return $this->getRender();
}
/** /**
* Return this item rendered to HTML * Return this item rendered to HTML
* *

View File

@ -347,9 +347,11 @@ class NavigationRenderer implements RecursiveIterator, NavigationRendererInterfa
{ {
foreach ($this as $item) { foreach ($this as $item) {
/** @var NavigationItem $item */ /** @var NavigationItem $item */
$this->content[] = $this->beginItemMarkup($item); if ($item->shouldRender()) {
$this->content[] = $item->render(); $this->content[] = $this->beginItemMarkup($item);
$this->content[] = $this->endItemMarkup(); $this->content[] = $item->render();
$this->content[] = $this->endItemMarkup();
}
} }
return join("\n", $this->content); return join("\n", $this->content);

View File

@ -129,10 +129,12 @@ class RecursiveNavigationRenderer extends RecursiveIteratorIterator implements N
{ {
foreach ($this as $item) { foreach ($this as $item) {
/** @var NavigationItem $item */ /** @var NavigationItem $item */
$this->content[] = $this->getInnerIterator()->beginItemMarkup($item); if ($item->shouldRender()) {
$this->content[] = $item->render(); $this->content[] = $this->getInnerIterator()->beginItemMarkup($item);
if (! $item->hasChildren()) { $this->content[] = $item->render();
$this->content[] = $this->getInnerIterator()->endItemMarkup(); if (! $item->hasChildren()) {
$this->content[] = $this->getInnerIterator()->endItemMarkup();
}
} }
} }