Skip to content

Commit

Permalink
Added new setting to enable filtering of primary navigation.
Browse files Browse the repository at this point in the history
This is useful when you want to add FilterCodes
to the primary navigation/custom menu..
  • Loading branch information
michael-milette committed Jun 12, 2024
1 parent f4f5fa3 commit 9e49b51
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 5 deletions.
73 changes: 73 additions & 0 deletions classes/output/primary_navigation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?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/>.

/**
* Core navigation.
*
* @package theme_trema
* @category navigation
* @copyright 2022-2024 TNG Consulting Inc. - {@link https://www.tngconsulting.ca/}
* @author Michael Milette
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace theme_trema\output;

use filter_manager;
use renderer_base;
use custom_menu;

class primary_navigation extends \core\navigation\output\primary {
/**
* Custom menu items reside on the same level as the original nodes.
* Fetch and convert the nodes to a standardised array.
*
* @param renderer_base $output
* @return array
*/
protected function get_custom_menu(renderer_base $output): array {
global $CFG;

// Early return if a custom menu does not exists.
if (empty($CFG->custommenuitems)) {
return [];
}

$custommenuitems = $CFG->custommenuitems;

// If setting is enabled, filter custom menu items but don't apply auto-linking filters.
if (!empty(get_config('theme_trema', 'navfilter'))) {
// Include default filters to skip.
$skipfilters = 'activitynames,data,glossary,sectionnames,bookchapters,urltolink,'
. get_config('theme_trema', 'navfiltersexcluded');
// Convert to array, trim all items, and remove duplicates and empty values.
$skipfilters = array_filter(array_unique(array_map('trim', explode(',', $skipfilters))));
$filteroptions = ['originalformat' => FORMAT_HTML, 'noclean' => true];
$filtermanager = filter_manager::instance();
$context = \context_system::instance();
$custommenuitems = $filtermanager->filter_text($custommenuitems, $context, $filteroptions, $skipfilters);
}

$currentlang = current_language();
$custommenunodes = custom_menu::convert_text_to_menu_nodes($custommenuitems, $currentlang);
$nodes = [];
foreach ($custommenunodes as $node) {
$nodes[] = $node->export_for_template($output);
}

return $nodes;
}
}
4 changes: 4 additions & 0 deletions lang/en/theme_trema.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@
$string['loginshowloginform'] = 'Show login form';
$string['loginshowloginform_desc'] = 'Before disabling the login form, ensure that administrators have an alternate method to Sign in to the site.';
$string['modal'] = 'Modal';
$string['navfilter'] = 'Filter navigation';
$string['navfilter_desc'] = 'Enable to process primary navigation through Moodle filters. Useful for dynamic custom menu items.';
$string['navfiltersexcluded'] = 'Excluded navigation filters';
$string['navfiltersexcluded_desc'] = 'These filters will not be applied to the primary navigation (custom menu). Must be a comma separated list of filter names. By default, the following auto-linking filters are always excluded: activitynames, data, glossary, sectionnames, bookchapters, urltolink.';
$string['numberofcards'] = 'Number of cards';
$string['numberofimages'] = "Number of images in frontpage banner";
$string['numberofimages_desc'] = "Number of images to show in frontpage banner. If it is more than one, loads the carousel.";
Expand Down
2 changes: 1 addition & 1 deletion layout/columns2.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
}
}

$primary = new core\navigation\output\primary($PAGE);
$primary = new theme_trema\output\primary_navigation($PAGE);
$renderer = $PAGE->get_renderer('core');
$primarymenu = $primary->export_for_template($renderer);
$buildregionmainsettings = !$PAGE->include_region_main_settings_in_header_actions() && !$PAGE->has_secondary_navigation();
Expand Down
2 changes: 1 addition & 1 deletion layout/drawers.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
}
}

$primary = new core\navigation\output\primary($PAGE);
$primary = new theme_trema\output\primary_navigation($PAGE);
$renderer = $PAGE->get_renderer('core');
$primarymenu = $primary->export_for_template($renderer);
$buildregionmainsettings = !$PAGE->include_region_main_settings_in_header_actions() && !$PAGE->has_secondary_navigation();
Expand Down
2 changes: 1 addition & 1 deletion layout/frontpage.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
}
}

$primary = new core\navigation\output\primary($PAGE);
$primary = new theme_trema\output\primary_navigation($PAGE);
$renderer = $PAGE->get_renderer('core');
$primarymenu = $primary->export_for_template($renderer);
$buildregionmainsettings = !$PAGE->include_region_main_settings_in_header_actions() && !$PAGE->has_secondary_navigation();
Expand Down
2 changes: 1 addition & 1 deletion layout/mydashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
}
}

$primary = new core\navigation\output\primary($PAGE);
$primary = new theme_trema\output\primary_navigation($PAGE);
$renderer = $PAGE->get_renderer('core');
$primarymenu = $primary->export_for_template($renderer);
$buildregionmainsettings = !$PAGE->include_region_main_settings_in_header_actions() && !$PAGE->has_secondary_navigation();
Expand Down
18 changes: 18 additions & 0 deletions settings/general_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@
$setting->set_updatedcallback('theme_reset_all_caches');
$page->add($setting);

// Process primary navigation (custom menu) through Moodle filters.
$name = 'theme_trema/navfilter';
$title = get_string('navfilter', 'theme_trema');
$description = get_string('navfilter_desc', 'theme_trema');
$default = true;
$setting = new admin_setting_configcheckbox($name, $title, $description, $default, true, false);
$setting->set_updatedcallback('theme_reset_all_caches');
$page->add($setting);

// List of filters that should not be applied to the primary navigation (custom menu).
$name = 'theme_trema/navfiltersexcluded';
$title = get_string('navfiltersexcluded', 'theme_trema');
$description = get_string('navfiltersexcluded_desc', 'theme_trema');
$default = '';
$setting = new admin_setting_configtext($name, $title, $description, $default, PARAM_TEXT);
$setting->set_updatedcallback('theme_reset_all_caches');
$page->add($setting);

// Hide selected items in the primary navigation (custom menu).
$hideitemsoptions = [];
$hideitemsoptions['home'] = get_string('home');
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2024052602;
$plugin->version = 2024061100;
$plugin->release = '4.4.0.4';
$plugin->maturity = MATURITY_STABLE;
$plugin->requires = 2022021800; // Moodle 4.0 - Build: 20220218.
Expand Down

0 comments on commit 9e49b51

Please sign in to comment.