NavigationItem: Provide the item to the renderer before calling render

refs #5600
This commit is contained in:
Johannes Meyer 2015-09-03 14:22:38 +02:00
parent 16ece09b0a
commit 9a725330fb
2 changed files with 42 additions and 2 deletions

View File

@ -667,7 +667,7 @@ class NavigationItem implements IteratorAggregate
*/ */
public function render() public function render()
{ {
$this->getRenderer()->render($this); $this->getRenderer()->setItem($this)->render();
} }
/** /**

View File

@ -4,6 +4,7 @@
namespace Icinga\Web\Navigation\Renderer; namespace Icinga\Web\Navigation\Renderer;
use Icinga\Application\Icinga; use Icinga\Application\Icinga;
use Icinga\Exception\ProgrammingError;
use Icinga\Util\String; use Icinga\Util\String;
use Icinga\Web\Navigation\NavigationItem; use Icinga\Web\Navigation\NavigationItem;
use Icinga\Web\View; use Icinga\Web\View;
@ -20,6 +21,13 @@ class NavigationItemRenderer
*/ */
protected $view; protected $view;
/**
* The item being rendered
*
* @var NavigationItem
*/
protected $item;
/** /**
* The link target * The link target
* *
@ -83,6 +91,29 @@ class NavigationItemRenderer
return $this->view; return $this->view;
} }
/**
* Set the navigation item to render
*
* @param NavigationItem $item
*
* @return $this
*/
public function setItem(NavigationItem $item)
{
$this->item = $item;
return $this;
}
/**
* Return the navigation item being rendered
*
* @return NavigationItem
*/
public function getItem()
{
return $this->item;
}
/** /**
* Set the link target * Set the link target
* *
@ -113,8 +144,17 @@ class NavigationItemRenderer
* *
* @return string * @return string
*/ */
public function render(NavigationItem $item) public function render(NavigationItem $item = null)
{ {
if ($item !== null) {
$this->setItem($item);
} elseif (($item = $this->getItem()) === null) {
throw new ProgrammingError(
'Cannot render nothing. Pass the item to render as part'
. ' of the call to render() or set it with setItem()'
);
}
$label = $this->view()->escape($item->getLabel()); $label = $this->view()->escape($item->getLabel());
if (($icon = $item->getIcon()) !== null) { if (($icon = $item->getIcon()) !== null) {
$label = $this->view()->icon($icon) . $label; $label = $this->view()->icon($icon) . $label;