MDL-81047 core: Standardise init of before_standard_head_html_generation

This commit is contained in:
Andrew Nicols 2024-03-07 11:24:59 +08:00
parent 0a30867cdf
commit 017e52f213
No known key found for this signature in database
GPG Key ID: 6D1E3157C8CFBF14
2 changed files with 28 additions and 14 deletions

View File

@ -26,19 +26,25 @@ namespace core\hook\output;
#[\core\attribute\tags('output')]
#[\core\attribute\label('Allows plugins to add any elements to the page <head> html tag.')]
#[\core\attribute\hook\replaces_callbacks('before_standard_html_head')]
class before_standard_head_html_generation {
/** @var string $output Stores results from callbacks */
private $output = '';
final class before_standard_head_html_generation {
public function __construct(
/** @var \renderer_base The core_renderer instnace used for the generation */
public readonly \renderer_base $renderer,
private string $output = '',
) {
}
/**
* Plugins implementing callback can add any HTML to the page.
*
* Must be a string containing valid html head content
*
* @param string $output
* @param null|string $output
*/
public function add_html(string $output): void {
$this->output .= $output;
public function add_html(?string $output): void {
if ($output) {
$this->output .= $output;
}
}
/**

View File

@ -35,6 +35,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core\di;
use core\hook\manager as hook_manager;
use core\output\named_templatable;
use core_completion\cm_completion_details;
use core_course\output\activity_information;
@ -701,28 +703,34 @@ class core_renderer extends renderer_base {
// Give plugins an opportunity to add any head elements. The callback
// must always return a string containing valid html head content.
$hook = new \core\hook\output\before_standard_head_html_generation();
\core\hook\manager::get_instance()->dispatch($hook);
$hook = new \core\hook\output\before_standard_head_html_generation($this);
di::get(hook_manager::class)->dispatch($hook);
$hook->process_legacy_callbacks();
$output = $hook->get_output();
// Allow a url_rewrite plugin to setup any dynamic head content.
if (isset($CFG->urlrewriteclass) && !isset($CFG->upgraderunning)) {
$class = $CFG->urlrewriteclass;
$output .= $class::html_head_setup();
$hook->add_html($class::html_head_setup());
}
$output .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' . "\n";
$output .= '<meta name="keywords" content="moodle, ' . $this->page->title . '" />' . "\n";
$hook->add_html('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' . "\n");
$hook->add_html('<meta name="keywords" content="moodle, ' . $this->page->title . '" />' . "\n");
// This is only set by the {@link redirect()} method
$output .= $this->metarefreshtag;
$hook->add_html($this->metarefreshtag);
// Check if a periodic refresh delay has been set and make sure we arn't
// already meta refreshing
if ($this->metarefreshtag=='' && $this->page->periodicrefreshdelay!==null) {
$output .= '<meta http-equiv="refresh" content="'.$this->page->periodicrefreshdelay.';url='.$this->page->url->out().'" />';
$hook->add_html(
html_writer::empty_tag('meta', [
'http-equiv' => 'refresh',
'content' => $this->page->periodicrefreshdelay . ';url='.$this->page->url->out(),
]),
);
}
$output = $hook->get_output();
// Set up help link popups for all links with the helptooltip class
$this->page->requires->js_init_call('M.util.help_popups.setup');