Skip to content

Commit

Permalink
MDL-71823 user: Add hook for extending user menu
Browse files Browse the repository at this point in the history
  • Loading branch information
gbarat87 committed Jul 17, 2024
1 parent fd487cd commit 3a8b6f3
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
57 changes: 57 additions & 0 deletions user/classes/hook/update_navigation_menuuser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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;

/**
* 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 array $navitems = [],
) {
}

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

/**
* Returns a class with the detail for the menu.
*
* @return array
*/
public function get_navitems(): array {
return $this->navitems;
}
}
14 changes: 14 additions & 0 deletions user/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

use core\di;
use core\hook;
use core_user\hook\update_navigation_menuuser;

define('USER_FILTER_ENROLMENT', 1);
define('USER_FILTER_GROUP', 2);
define('USER_FILTER_LAST_ACCESS', 3);
Expand Down Expand Up @@ -909,6 +913,16 @@ function user_get_user_navigation_info($user, $page, $options = array()) {
}
}

// Call to hook to add menu items.
$hook = new update_navigation_menuuser();
di::get(core\hook\manager::class)->dispatch($hook);
$hookitems = $hook->get_navitems();
foreach ($hookitems as $menuitem) {
if (property_exists($menuitem, 'itemtype')) {
$returnobject->navitems[] = $menuitem;
}
}

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

0 comments on commit 3a8b6f3

Please sign in to comment.