Skip to content

Commit

Permalink
Add call to hook update_navigation_item fix #215 (#221)
Browse files Browse the repository at this point in the history
* Add call to hook update_navigation_item and add env in menu. Closes #215

* Restrict view of environment list, reorder list

* Add current link

* Remove previous unused settings, add enable menu control
  • Loading branch information
gbarat87 authored Dec 11, 2024
1 parent b021dff commit e59e2af
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 100 deletions.
35 changes: 35 additions & 0 deletions classes/hook_callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

require_once($CFG->dirroot . '/local/envbar/lib.php');

use context_system;
use core\hook\output\before_standard_top_of_body_html_generation;
use core_user\hook\extend_user_menu;
use local_envbar\local\envbarlib;

/**
Expand All @@ -43,6 +45,39 @@ public static function before_standard_top_of_body_html_generation(before_standa
$hook->add_html(envbarlib::get_inject_code());
}

/**
* This is the hook enables the plugin to add one or more menu item.
*
* @param extend_user_menu $hook
*/
public static function extend_user_menu(extend_user_menu $hook): void {
global $CFG;

$config = get_config('local_envbar');
if (empty($config->enablemenu)) {
return;
}

$prodwwwroot = envbarlib::getprodwwwroot();
// Do not display on the production environment!
if ($prodwwwroot === $CFG->wwwroot) {
return;
}

// If the prodwwwroot is not set, only show the bar to admin users.
if (empty($prodwwwroot)) {
if (!has_capability('moodle/site:config', context_system::instance())) {
return;
}
}

// Get items to add.
$navitems = envbarlib::add_menuuser();
foreach ($navitems as $item) {
$hook->add_navitem($item);
}
}

/**
* Listener for the after_config hook.
*
Expand Down
35 changes: 35 additions & 0 deletions classes/local/envbarlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -684,4 +684,39 @@ public static function config() {
}
}

/**
* Add items to the menu navigation.
*
* @return array New menu items.
*/
public static function add_menuuser(): array {
global $PAGE;
$userfirstmenu = new stdClass();
$userfirstmenu->itemtype = 'divider';
$here = (new moodle_url('/'))->out();
$prodwwwroot = self::getprodwwwroot();
// Get prod Environment.
$prodenv = new stdClass();
$prodenv->matchpattern = $prodwwwroot;
$prodenv->showtext = get_string('prod', 'local_envbar');
$envsprod[] = $prodenv;
// Attached the list of Environments to the prod one.
$envslistfinal = array_merge($envsprod, self::get_records());
$navitem[] = $userfirstmenu;
foreach ($envslistfinal as $env) {
$usermenu = new stdClass();
$usermenu->itemtype = 'link';
$usermenu->title = $env->showtext;
$pathurl = (new moodle_url( $PAGE->__get('url')))->get_path();
$currenturl = $env->matchpattern.$pathurl;
$usermenu->url = new moodle_url($currenturl);
// Which env matches?
if (self::is_match($here, $env->matchpattern)) {
$usermenu->pix = 'e/tick';
}
$navitem[] = $usermenu;
}
return $navitem;
}

}
5 changes: 5 additions & 0 deletions db/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@
'hook' => \core\hook\after_config::class,
'callback' => [\local_envbar\hook_callbacks::class, 'after_config'],
],
[
'hook' => core_user\hook\extend_user_menu::class,
'callback' => '\local_envbar\hook_callbacks::extend_user_menu',
'priority' => 0,
],
];
4 changes: 0 additions & 4 deletions lang/en/local_envbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
$string['debuggingon'] = 'Debugging On';
$string['debugtogglelinkoff'] = 'Turn Off';
$string['debugtogglelinkon'] = 'Turn On';
$string['dividerselector'] = 'Divider selector';
$string['dividerselector_desc'] = 'This is a css class that is used for the menu divider element. If your theme uses different html then you may need to adjust this selector.<br />This setting is only used if the environment swapper menu is enabled.';
$string['emailheading'] = 'Email prefix';
$string['enableemailprefix'] = 'Enable email subject prefixing';
$string['enableemailprefix_desc'] = 'This setting controls if email subject fields will be prefixed with the first 4 letters of the environment\'s name or not.';
Expand All @@ -64,8 +62,6 @@
$string['menuheading'] = 'Environment swapper menu';
$string['menulastrefresh'] = 'Last refresh';
$string['menupresentation'] = 'Presentation';
$string['menuselector'] = 'Menu selector';
$string['menuselector_desc'] = 'This is a css or xpath selector to find the menu ul element for injecting the env swapper menu. If your theme uses different html then you may need to adjust this selector.<br />This setting is only used if the environment swapper menu is enabled.';
$string['missing_required_parameter'] = 'A required parameter was missing. Required params are wwwroot and lastrefresh.';
$string['nextrefreshin'] = 'Reset in {$a}';
$string['notconfigured'] = 'UNKNOWN';
Expand Down
84 changes: 0 additions & 84 deletions renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ public function render_envbar($match, $fixed = true, $envs = array()) {

if ($fixed) {
$js .= local_envbar_favicon_js($match);
$js .= local_envbar_user_menu($envs, $match);
$js .= local_envbar_title($match);
}

Expand Down Expand Up @@ -384,86 +383,3 @@ function local_envbar_favicon_js($match) {

return $js;
}

/**
* Gets some JS which inserts env jump links into the user menu
*
* @param array $envs
* @return string A chunk of JS
*/
function local_envbar_user_menu($envs) {

global $CFG, $PAGE;

$config = get_config('local_envbar');

if (empty($config->enablemenu)) {
return '';
}

if (isset($config->menuselector)) {
$menuselector = $config->menuselector;
} else {
$menuselector = '.usermenu .menu';
}

if (empty($menuselector)) {
return ''; // Not using user menu, nothing to do.
}

$html = '';

if ($PAGE->has_set_url()) {
$url = $PAGE->url->out();

foreach ($envs as $env) {
$jump = $url;
$jump = str_replace($CFG->wwwroot, $env->matchpattern, $jump);
if ($jump == $url) {
continue;
}
$jump = s($jump);
$show = s($env->showtext);
$link = <<<EOD
<li role="presentation">
<a class="icon menu-action no-envbar-highlight" role="menuitem" href="{$jump}">
<span class="menu-action-text">$show</span>
</a>
</li>
EOD;
$html .= $link;
}
}

if (!$html) {
return '';
}

if (isset($config->dividerselector)) {
$divider = $config->dividerselector;
} else {
$divider = 'filler';
}
$html = '<li role="presentation"><span class="'. $divider .'">&nbsp;</span></li>' . $html;

$html = str_replace("\n", '', $html);
$html = str_replace("\"", "\\\"", $html);

$url = (new moodle_url('/admin/settings.php?section=local_envbar_presentation'))->out();
$isadmin = (is_siteadmin() ) ? '1' : '0';

$js = <<<EOD
var menu = document.querySelector('$menuselector');
var html = "$html";
if (menu) {
menu.insertAdjacentHTML('beforeend', html);
} else {
$isadmin && console.error(
"local_envbar: Menu selector is misconfigured '$menuselector' \\n Please configure it here: $url");
}
EOD;
return $js;

}

12 changes: 0 additions & 12 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,6 @@
get_string('enablemenu_desc', 'local_envbar', null, true),
true));

$presentation->add(new admin_setting_configtext('local_envbar/menuselector',
get_string('menuselector', 'local_envbar', null, true),
get_string('menuselector_desc', 'local_envbar', null, true),
'.usermenu .menu',
PARAM_RAW));

$presentation->add(new admin_setting_configtext('local_envbar/dividerselector',
get_string('dividerselector', 'local_envbar', null, true),
get_string('dividerselector_desc', 'local_envbar', null, true),
'filler',
PARAM_RAW));

$presentation->add(new admin_setting_heading('local_envbar/linksheading',
get_string('linksheading', 'local_envbar', null, true),
''));
Expand Down

0 comments on commit e59e2af

Please sign in to comment.