From c16b882bcd9bc87fe6268fa3e3e8874aaabb7203 Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Thu, 8 Aug 2013 12:26:52 +1200 Subject: [PATCH] MDL-41081 navigation: added means to prepend to the navbar. --- lib/navigationlib.php | 59 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/lib/navigationlib.php b/lib/navigationlib.php index 3a897d78361..4b40708bf7e 100644 --- a/lib/navigationlib.php +++ b/lib/navigationlib.php @@ -2944,6 +2944,8 @@ class navbar extends navigation_node { public $children = array(); /** @var bool A switch for whether we want to include the root node in the navbar */ public $includesettingsbase = false; + /** @var navigation_node[] $prependchildren */ + protected $prependchildren = array(); /** * The almighty constructor * @@ -2979,7 +2981,7 @@ class navbar extends navigation_node { $activenodefound = ($this->page->navigation->contains_active_node() || $this->page->settingsnav->contains_active_node()); - $outcome = (count($this->children)>0 || (!$this->ignoreactive && $activenodefound)); + $outcome = (count($this->children) > 0 || count($this->prependchildren) || (!$this->ignoreactive && $activenodefound)); $this->hasitems = $outcome; return $outcome; } @@ -3006,6 +3008,11 @@ class navbar extends navigation_node { return $child; } } + foreach ($this->prependchildren as &$child) { + if ($child->key === $key && ($type == null || $type == $child->type)) { + return $child; + } + } return false; } /** @@ -3089,6 +3096,11 @@ class navbar extends navigation_node { 'action'=>$this->page->navigation->action )); + if (count($this->prependchildren) > 0) { + // Add the custom children + $items = array_merge($items, array_reverse($this->prependchildren)); + } + $this->items = array_reverse($items); return $this->items; } @@ -3176,6 +3188,51 @@ class navbar extends navigation_node { $this->children[] = new navigation_node($itemarray); return $this; } + + /** + * Prepends a new navigation_node to the start of the navbar + * + * @param string $text + * @param string|moodle_url|action_link $action An action to associate with this node. + * @param int $type One of navigation_node::TYPE_* + * @param string $shorttext + * @param string|int $key A key to identify this node with. Key + type is unique to a parent. + * @param pix_icon $icon An optional icon to use for this node. + * @return navigation_node + */ + public function prepend($text, $action=null, $type=self::TYPE_CUSTOM, $shorttext=null, $key=null, pix_icon $icon=null) { + if ($this->content !== null) { + debugging('Nav bar items must be printed before $OUTPUT->header() has been called', DEBUG_DEVELOPER); + } + // Properties array used when creating the new navigation node + $itemarray = array( + 'text' => $text, + 'type' => $type + ); + // Set the action if one was provided + if ($action!==null) { + $itemarray['action'] = $action; + } + // Set the shorttext if one was provided + if ($shorttext!==null) { + $itemarray['shorttext'] = $shorttext; + } + // Set the icon if one was provided + if ($icon!==null) { + $itemarray['icon'] = $icon; + } + // Default the key to the number of children if not provided + if ($key === null) { + $key = count($this->children); + } + // Set the key + $itemarray['key'] = $key; + // Set the parent to this node + $itemarray['parent'] = $this; + // Add the child node to the prepend list. + $this->prependchildren[] = new navigation_node($itemarray); + return $this; + } } /** -- 2.43.0