diff --git a/lib/navigationlib.php b/lib/navigationlib.php index f56a6ef65c7..09cfc188f1e 100644 --- a/lib/navigationlib.php +++ b/lib/navigationlib.php @@ -116,6 +116,8 @@ class navigation_node implements renderable { public $forceopen = false; /** @var array An array of CSS classes for the node */ public $classes = array(); + /** @var array An array of HTML attributes for the node */ + public $attributes = []; /** @var navigation_node_collection An array of child nodes */ public $children = array(); /** @var bool If set to true the node will be recognised as active */ @@ -559,6 +561,16 @@ class navigation_node implements renderable { return true; } + /** + * Adds an HTML attribute to this node. + * + * @param string $name + * @param string $value + */ + public function add_attribute(string $name, string $value): void { + $this->attributes[] = ['name' => $name, 'value' => $value]; + } + /** * Removes a CSS class from this node. * @@ -2290,6 +2302,7 @@ class global_navigation extends navigation_node { null, $section->id, new pix_icon('i/section', '')); $sectionnode->nodetype = navigation_node::NODETYPE_BRANCH; $sectionnode->hidden = (!$section->visible || !$section->available); + $sectionnode->add_attribute('data-section-name-for', $section->id); if ($this->includesectionnum !== false && $this->includesectionnum == $section->section) { $this->load_section_activities($sectionnode, $section->section, $activities); } diff --git a/lib/templates/navbar.mustache b/lib/templates/navbar.mustache index c1834d84c19..6e94273bfe4 100644 --- a/lib/templates/navbar.mustache +++ b/lib/templates/navbar.mustache @@ -68,11 +68,21 @@ }}{{#get_items}} {{#has_action}} {{/has_action}} {{^has_action}} - + {{/has_action}} {{/get_items}}{{! }} diff --git a/lib/tests/navigationlib_test.php b/lib/tests/navigationlib_test.php index ed875a7a8e1..dc38ebfcfb9 100644 --- a/lib/tests/navigationlib_test.php +++ b/lib/tests/navigationlib_test.php @@ -152,6 +152,22 @@ class navigationlib_test extends \advanced_testcase { } } + /** + * Test the add_attribute method. + * @covers \navigation_node::add_attribute + */ + public function test_node_add_attribute(): void { + $this->setup_node(); + + $node = $this->node->get('demo1'); + $this->assertInstanceOf('navigation_node', $node); + if ($node !== false) { + $node->add_attribute('data-foo', 'bar'); + $attribute = reset($node->attributes); + $this->assertEqualsCanonicalizing(['name' => 'data-foo', 'value' => 'bar'], $attribute); + } + } + public function test_node_check_if_active() { $this->setup_node(); diff --git a/lib/upgrade.txt b/lib/upgrade.txt index 3d02d603ede..e93d1c78735 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -92,6 +92,7 @@ information provided here is intended especially for developers. - `question_fix_top_names` * Added a new parameter to `core_renderer::container` and `core_renderer::container_start` to allow for the addition of custom attributes. +* Added a new method `navigation_node::add_attribute()` to allow adding HTML attributes to the node. === 4.3 ===