2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 namespace core\hook\output
;
20 * Class before_html_attributes
23 * @copyright 2024 Andrew Lyons <andrew@nicols.co.uk>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 * @property-read \renderer_base $renderer The page renderer object
26 * @property array $attributes The list of HTML attributes to be added to the tag.
28 #[\core\attribute\tags('output')]
29 #[\core\attribute\label('Allows plugins to add, remove or modify any attributes of the html tag.')]
30 #[\core\attribute\hook\replaces_callbacks('add_htmlattributes')]
31 final class before_html_attributes
{
33 * Constructor for the before_html_attributes hook.
35 * @param \renderer_base $renderer The page renderer object
36 * @param array $attributes The list of HTML attributes initially on the tag
38 public function __construct(
39 /** @var \renderer_base The page renderer */
40 public readonly \renderer_base
$renderer,
41 /** @var array The list of HTML attributes initially on the tag */
42 private array $attributes = [],
47 * Add an HTML attribute to the list.
50 * @param string $value
52 public function add_attribute(string $name, string $value): void
{
53 $this->attributes
[$name] = $value;
57 * Get the list of attributes.
61 public function get_attributes(): array {
62 return $this->attributes
;
66 * Remove an HTML attribute from the list.
70 public function remove_attribute(string $name): void
{
71 unset($this->attributes
[$name]);
75 * Process legacy callbacks.
77 public function process_legacy_callbacks(): void
{
78 // Legacy callback 'add_htmlattributes' is deprecated since Moodle 4.4.
80 // This function should return an array of html attribute names => values.
81 $pluginswithfunction = get_plugins_with_function(
82 function: 'add_htmlattributes',
85 foreach ($pluginswithfunction as $plugins) {
86 foreach ($plugins as $function) {
87 $newattrs = $function();
88 unset($newattrs['dir']);
89 unset($newattrs['lang']);
90 unset($newattrs['xmlns']);
91 unset($newattrs['xml:lang']);
92 foreach ($newattrs as $name => $value) {
93 $this->add_attribute($name, $value);