From 3cacf970bd72684cad0b72e1ba7fa4eea394112b Mon Sep 17 00:00:00 2001 From: Guillaume Barat Date: Tue, 16 Jul 2024 21:12:13 +1000 Subject: [PATCH] Add Hook navigation user --- .gitmodules | 3 + local/envbar | 1 + .../hook/update_navigation_menuuser.php | 74 +++++++++++++++++++ user/lib.php | 8 ++ 4 files changed, 86 insertions(+) create mode 100644 .gitmodules create mode 160000 local/envbar create mode 100644 user/classes/hook/update_navigation_menuuser.php diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000000000..5f33b70c7eeca --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "local/envbar"] + path = local/envbar + url = git@github.com:catalyst/moodle-local_envbar.git diff --git a/local/envbar b/local/envbar new file mode 160000 index 0000000000000..2081682617371 --- /dev/null +++ b/local/envbar @@ -0,0 +1 @@ +Subproject commit 2081682617371f56986a458ba9b392b83da73034 diff --git a/user/classes/hook/update_navigation_menuuser.php b/user/classes/hook/update_navigation_menuuser.php new file mode 100644 index 0000000000000..2ea4f581d7127 --- /dev/null +++ b/user/classes/hook/update_navigation_menuuser.php @@ -0,0 +1,74 @@ +. + +namespace core_user\hook; + +use stdClass; + +/** + * Hook to modify user menu. + * + * @package core + * @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 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); + } + } + } + } +} diff --git a/user/lib.php b/user/lib.php index e975673301aea..58b538d3eb3fe 100644 --- a/user/lib.php +++ b/user/lib.php @@ -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();