MDL-81050 navigation: Add data attributes to sections in breadcrumbs

- Add a new method 'add_attribute' to navigation nodes to add HTML attributes to nodes.
- Add data-attribute to section nodes and include them in breadcrumb mustache to automatically update section titles
 when sections are renamed.
This commit is contained in:
Mikel Martín
2024-03-19 08:53:49 +01:00
parent de9c2393b2
commit dba1efa28e
4 changed files with 42 additions and 2 deletions
+13
View File
@@ -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);
}