Skip to content

Commit

Permalink
Add Hook navigation user
Browse files Browse the repository at this point in the history
  • Loading branch information
gbarat87 committed Jul 16, 2024
1 parent fd487cd commit 3cacf97
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "local/envbar"]
path = local/envbar
url = [email protected]:catalyst/moodle-local_envbar.git
1 change: 1 addition & 0 deletions local/envbar
Submodule envbar added at 208168
74 changes: 74 additions & 0 deletions user/classes/hook/update_navigation_menuuser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?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_user\hook;

use stdClass;

/**
* Hook to modify user menu.
*
* @package core
* @copyright 2024 Guillaume Barat <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @property-read \renderer_base $renderer The page renderer object
*/
#[\core\attribute\tags('user')]
#[\core\attribute\label('Allows plugins to add any elements to the user menu')]
final class update_navigation_menuuser {
/**
* Hook to modify user menu.
*
*/
public function __construct(
public stdClass $navitems = new stdClass(),
) {
}

/**
*
* @param null|stdClass $output
*/
public function add_navitems(?stdClass $output): void {
if ($output) {
$this->navitems = $output;
}
}

/**
* Returns a class with the deatil for the menu.
*
* @return stdClass
*/
public function get_navitems(): stdClass {
return $this->navitems;
}

/**
* Process legacy callbacks.
*/
public function process_legacy_callbacks(): void {
$pluginswithfunction = get_plugins_with_function(function: 'update_navigation_menuuser');
foreach ($pluginswithfunction as $plugins) {
foreach ($plugins as $function) {
$menuitem = $function();
if (is_object($menuitem)) {
$this->add_navitems($menuitem);
}
}
}
}
}
8 changes: 8 additions & 0 deletions user/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,14 @@ function user_get_user_navigation_info($user, $page, $options = array()) {
}
}

// Call to hook.
$hook = new \core_user\hook\update_navigation_menuuser();
$hook->process_legacy_callbacks();
$hookitems = $hook->get_navitems();
if(property_exists($hookitems, 'itemtype')) {
$returnobject->navitems[] = $hookitems;
}

if ($custommenucount > 0) {
// Only add a divider if we have customusermenuitems.
$divider = new stdClass();
Expand Down

0 comments on commit 3cacf97

Please sign in to comment.