From b1ee12f72130e40192a9df25f749162c6d6595e8 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 17 Sep 2015 09:11:17 +0200 Subject: [PATCH] NavigationItem: Allow to dynamically decide whether to render an item refs #5600 --- .../Icinga/Web/Navigation/NavigationItem.php | 43 +++++++++++++++++++ .../Renderer/NavigationRenderer.php | 8 ++-- .../Renderer/RecursiveNavigationRenderer.php | 10 +++-- 3 files changed, 54 insertions(+), 7 deletions(-) diff --git a/library/Icinga/Web/Navigation/NavigationItem.php b/library/Icinga/Web/Navigation/NavigationItem.php index 01888c41d..a72390e48 100644 --- a/library/Icinga/Web/Navigation/NavigationItem.php +++ b/library/Icinga/Web/Navigation/NavigationItem.php @@ -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 * diff --git a/library/Icinga/Web/Navigation/Renderer/NavigationRenderer.php b/library/Icinga/Web/Navigation/Renderer/NavigationRenderer.php index 219df6e2c..4818f402e 100644 --- a/library/Icinga/Web/Navigation/Renderer/NavigationRenderer.php +++ b/library/Icinga/Web/Navigation/Renderer/NavigationRenderer.php @@ -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); diff --git a/library/Icinga/Web/Navigation/Renderer/RecursiveNavigationRenderer.php b/library/Icinga/Web/Navigation/Renderer/RecursiveNavigationRenderer.php index 70cf071ab..83b2207bb 100644 --- a/library/Icinga/Web/Navigation/Renderer/RecursiveNavigationRenderer.php +++ b/library/Icinga/Web/Navigation/Renderer/RecursiveNavigationRenderer.php @@ -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(); + } } }