NavigationItem: Allow to dynamically decide whether to render an item
refs #5600
This commit is contained in:
parent
2d9f20f2c5
commit
b1ee12f721
|
@ -108,6 +108,13 @@ class NavigationItem implements IteratorAggregate
|
|||
*/
|
||||
protected $renderer;
|
||||
|
||||
/**
|
||||
* Whether to render this item
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $render;
|
||||
|
||||
/**
|
||||
* Create a new NavigationItem
|
||||
*
|
||||
|
@ -117,6 +124,7 @@ class NavigationItem implements IteratorAggregate
|
|||
public function __construct($name, array $properties = null)
|
||||
{
|
||||
$this->setName($name);
|
||||
$this->render = true;
|
||||
$this->priority = 100;
|
||||
$this->children = new Navigation();
|
||||
|
||||
|
@ -689,6 +697,41 @@ class NavigationItem implements IteratorAggregate
|
|||
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
|
||||
*
|
||||
|
|
|
@ -347,9 +347,11 @@ class NavigationRenderer implements RecursiveIterator, NavigationRendererInterfa
|
|||
{
|
||||
foreach ($this as $item) {
|
||||
/** @var NavigationItem $item */
|
||||
$this->content[] = $this->beginItemMarkup($item);
|
||||
$this->content[] = $item->render();
|
||||
$this->content[] = $this->endItemMarkup();
|
||||
if ($item->shouldRender()) {
|
||||
$this->content[] = $this->beginItemMarkup($item);
|
||||
$this->content[] = $item->render();
|
||||
$this->content[] = $this->endItemMarkup();
|
||||
}
|
||||
}
|
||||
|
||||
return join("\n", $this->content);
|
||||
|
|
|
@ -129,10 +129,12 @@ class RecursiveNavigationRenderer extends RecursiveIteratorIterator implements N
|
|||
{
|
||||
foreach ($this as $item) {
|
||||
/** @var NavigationItem $item */
|
||||
$this->content[] = $this->getInnerIterator()->beginItemMarkup($item);
|
||||
$this->content[] = $item->render();
|
||||
if (! $item->hasChildren()) {
|
||||
$this->content[] = $this->getInnerIterator()->endItemMarkup();
|
||||
if ($item->shouldRender()) {
|
||||
$this->content[] = $this->getInnerIterator()->beginItemMarkup($item);
|
||||
$this->content[] = $item->render();
|
||||
if (! $item->hasChildren()) {
|
||||
$this->content[] = $this->getInnerIterator()->endItemMarkup();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue