From 5ebd1fb9768969956c9131df4274f9cdff7f0134 Mon Sep 17 00:00:00 2001 From: Brendan Heywood Date: Thu, 6 Oct 2016 22:27:58 +1100 Subject: [PATCH] MDL-53978 core: Added callbacks for all major render stages --- lib/outputrenderers.php | 60 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/lib/outputrenderers.php b/lib/outputrenderers.php index 977fa6984e5..550b09887a1 100644 --- a/lib/outputrenderers.php +++ b/lib/outputrenderers.php @@ -513,9 +513,30 @@ class core_renderer extends renderer_base { */ public function htmlattributes() { $return = get_html_lang(true); + $attributes = array(); if ($this->page->theme->doctype !== 'html5') { - $return .= ' xmlns="http://www.w3.org/1999/xhtml"'; + $attributes['xmlns'] = 'http://www.w3.org/1999/xhtml'; } + + // Give plugins an opportunity to add things like xml namespaces to the html element. + // This function should return an array of html attribute names => values. + $pluginswithfunction = get_plugins_with_function('add_htmlattributes', 'lib.php'); + foreach ($pluginswithfunction as $plugins) { + foreach ($plugins as $function) { + $newattrs = $function(); + unset($newattrs['dir']); + unset($newattrs['lang']); + unset($newattrs['xmlns']); + unset($newattrs['xml:lang']); + $attributes += $newattrs; + } + } + + foreach ($attributes as $key => $val) { + $val = s($val); + $return .= " $key=\"$val\""; + } + return $return; } @@ -540,6 +561,15 @@ class core_renderer extends renderer_base { $output = ''; + // Give plugins an opportunity to add any head elements. The callback + // must always return a string containing valid html head content. + $pluginswithfunction = get_plugins_with_function('before_standard_html_head', 'lib.php'); + foreach ($pluginswithfunction as $plugins) { + foreach ($plugins as $function) { + $output .= $function(); + } + } + // Allow a url_rewrite plugin to setup any dynamic head content. if (isset($CFG->urlrewriteclass) && !isset($CFG->upgraderunning)) { $class = $CFG->urlrewriteclass; @@ -628,7 +658,18 @@ class core_renderer extends renderer_base { if ($this->page->pagelayout !== 'embedded' && !empty($CFG->additionalhtmltopofbody)) { $output .= "\n".$CFG->additionalhtmltopofbody; } + + // Give plugins an opportunity to inject extra html content. The callback + // must always return a string containing valid html. + $pluginswithfunction = get_plugins_with_function('before_standard_top_of_body_html', 'lib.php'); + foreach ($pluginswithfunction as $plugins) { + foreach ($plugins as $function) { + $output .= $function(); + } + } + $output .= $this->maintenance_warning(); + return $output; } @@ -1001,6 +1042,15 @@ class core_renderer extends renderer_base { public function header() { global $USER, $CFG, $SESSION; + // Give plugins an opportunity touch things before the http headers are sent + // such as adding additional headers. The return value is ignored. + $pluginswithfunction = get_plugins_with_function('before_http_headers', 'lib.php'); + foreach ($pluginswithfunction as $plugins) { + foreach ($plugins as $function) { + $function(); + } + } + if (\core\session\manager::is_loggedinas()) { $this->page->add_body_class('userloggedinas'); } @@ -1112,6 +1162,14 @@ class core_renderer extends renderer_base { public function footer() { global $CFG, $DB, $PAGE; + // Give plugins an opportunity to touch the page before JS is finalized. + $pluginswithfunction = get_plugins_with_function('before_footer', 'lib.php'); + foreach ($pluginswithfunction as $plugins) { + foreach ($plugins as $function) { + $function(); + } + } + $output = $this->container_end_all(true); $footer = $this->opencontainers->pop('header/footer');