1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
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.
|
|
|
8 |
//
|
|
|
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.
|
|
|
13 |
//
|
|
|
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/>.
|
|
|
16 |
|
|
|
17 |
namespace core\hook\output;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Class before_html_attributes
|
|
|
21 |
*
|
|
|
22 |
* @package core
|
|
|
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.
|
|
|
27 |
*/
|
|
|
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 {
|
|
|
32 |
/**
|
|
|
33 |
* Constructor for the before_html_attributes hook.
|
|
|
34 |
*
|
|
|
35 |
* @param \renderer_base $renderer The page renderer object
|
|
|
36 |
* @param array $attributes The list of HTML attributes initially on the tag
|
|
|
37 |
*/
|
|
|
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 = [],
|
|
|
43 |
) {
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Add an HTML attribute to the list.
|
|
|
48 |
*
|
|
|
49 |
* @param string $name
|
|
|
50 |
* @param string $value
|
|
|
51 |
*/
|
|
|
52 |
public function add_attribute(string $name, string $value): void {
|
|
|
53 |
$this->attributes[$name] = $value;
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Get the list of attributes.
|
|
|
58 |
*
|
|
|
59 |
* @return array
|
|
|
60 |
*/
|
|
|
61 |
public function get_attributes(): array {
|
|
|
62 |
return $this->attributes;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Remove an HTML attribute from the list.
|
|
|
67 |
*
|
|
|
68 |
* @param string $name
|
|
|
69 |
*/
|
|
|
70 |
public function remove_attribute(string $name): void {
|
|
|
71 |
unset($this->attributes[$name]);
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* Process legacy callbacks.
|
|
|
76 |
*/
|
|
|
77 |
public function process_legacy_callbacks(): void {
|
|
|
78 |
// Legacy callback 'add_htmlattributes' is deprecated since Moodle 4.4.
|
|
|
79 |
|
|
|
80 |
// This function should return an array of html attribute names => values.
|
|
|
81 |
$pluginswithfunction = get_plugins_with_function(
|
|
|
82 |
function: 'add_htmlattributes',
|
|
|
83 |
migratedtohook: true,
|
|
|
84 |
);
|
|
|
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);
|
|
|
94 |
}
|
|
|
95 |
}
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
}
|