diff --git a/user/classes/hook/extend_user_menu.php b/user/classes/hook/extend_user_menu.php new file mode 100644 index 0000000000000..ec8d32f33ad57 --- /dev/null +++ b/user/classes/hook/extend_user_menu.php @@ -0,0 +1,58 @@ +. + +namespace core_user\hook; + +/** + * Hook to modify user menu. + * + * @package core_user + * @copyright 2024 Guillaume Barat + * @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 extend_user_menu { + /** + * Hook to modify user menu. + * + * @param array $navitems Menu item to add. + */ + 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; + } +} diff --git a/user/lib.php b/user/lib.php index e975673301aea..25095974b2306 100644 --- a/user/lib.php +++ b/user/lib.php @@ -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\extend_user_menu; + define('USER_FILTER_ENROLMENT', 1); define('USER_FILTER_GROUP', 2); define('USER_FILTER_LAST_ACCESS', 3); @@ -909,6 +913,16 @@ function user_get_user_navigation_info($user, $page, $options = array()) { } } + // Call to hook to add menu items. + $hook = new extend_user_menu(); + 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();