Skip to content

Commit

Permalink
MDL-79088 navigation: new hook to add items to site primary navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Sep 4, 2023
1 parent 206c3a6 commit 906dfc2
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
65 changes: 65 additions & 0 deletions lib/classes/hook/navigation/primary_extend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace core\hook\navigation;

use core\hook\described_hook;
use core\navigation\views\primary;

/**
* Allows plugins to insert nodes into site primary navigation
*
* @package core
* @copyright 2023 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class primary_extend implements described_hook {

/**
* Creates new hook.
*
* @param primary $primaryview Primary navigation view
*/
public function __construct(protected primary $primaryview) {
}

/**
* Primary navigation view
*
* @return primary
*/
public function get_primaryview(): primary {
return $this->primaryview;
}

/**
* Describes the hook purpose.
*
* @return string
*/
public static function get_hook_description(): string {
return 'Allows plugins to insert nodes into site primary navigation';
}

/**
* List of tags that describe this hook.
*
* @return string[]
*/
public static function get_hook_tags(): array {
return ['navigation'];
}
}
19 changes: 16 additions & 3 deletions lib/classes/navigation/output/primary.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,31 @@ public function export_for_template(?renderer_base $output = null): array {
/**
* Get the primary nav object and standardize the output
*
* @param \navigation_node|null $parent used for nested nodes, by default the primarynav node
* @return array
*/
protected function get_primary_nav(): array {
protected function get_primary_nav($parent = null): array {
if ($parent === null) {
$parent = $this->page->primarynav;
}
$nodes = [];
foreach ($this->page->primarynav->children as $node) {
foreach ($parent->children as $node) {
$children = $this->get_primary_nav($node);
$activechildren = array_filter($children, function($child) {
return !empty($child['isactive']);
});
if ($node->preceedwithhr && count($nodes) && empty($nodes[count($nodes) - 1]['divider'])) {
$nodes[] = ['divider' => true];
}
$nodes[] = [
'title' => $node->get_title(),
'url' => $node->action(),
'text' => $node->text,
'icon' => $node->icon,
'isactive' => $node->isactive,
'isactive' => $node->isactive || !empty($activechildren),
'key' => $node->key,
'children' => $children,
'haschildren' => !empty($children) ? 1 : 0,
];
}

Expand Down
4 changes: 4 additions & 0 deletions lib/classes/navigation/views/primary.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public function initialise(): void {
$this->add($node->text, $node->action(), self::TYPE_SITE_ADMIN, null, 'siteadminnode', $node->icon);
}

// Allow plugins to add nodes to the primary navigation.
$hook = new \core\hook\navigation\primary_extend($this);
\core\hook\manager::get_instance()->dispatch($hook);

// Search and set the active node.
$this->set_active_node();
$this->initialised = true;
Expand Down

0 comments on commit 906dfc2

Please sign in to comment.