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);
}
+12 -2
View File
@@ -68,11 +68,21 @@
}}{{#get_items}}
{{#has_action}}
<li class="breadcrumb-item{{#is_hidden}} dimmed_text{{/is_hidden}}">
<a href="{{{action}}}" {{#is_last}}aria-current="page"{{/is_last}} {{#get_title}}title="{{get_title}}"{{/get_title}}>{{{get_content}}}</a>
<a href="{{{action}}}"
{{#is_last}}aria-current="page"{{/is_last}}
{{#get_title}}title="{{get_title}}"{{/get_title}}
{{#attributes}}{{name}}="{{value}}" {{/attributes}}
>
{{{get_content}}}
</a>
</li>
{{/has_action}}
{{^has_action}}
<li class="breadcrumb-item{{#is_hidden}} dimmed_text{{/is_hidden}}"><span>{{{text}}}</span></li>
<li class="breadcrumb-item{{#is_hidden}} dimmed_text{{/is_hidden}}">
<span {{#attributes}}{{name}}="{{value}}" {{/attributes}}>
{{{text}}}
</span>
</li>
{{/has_action}}
{{/get_items}}{{!
}}</ol>
+16
View File
@@ -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();
+1
View File
@@ -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 ===