Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Output callbacks: Difference between revisions

From MoodleDocs
(Created page with "There are cases where we want any plugin to contribute to a chunk of the output of any given page. We want this to be loosely coupled so you don't have say an admin tool's cod...")
 
 
(14 intermediate revisions by 5 users not shown)
Line 1: Line 1:
There are cases where we want any plugin to contribute to a chunk of the output of any given page. We want this to be loosely coupled so you don't have say an admin tool's code leaking into your theme. There are a variety of callbacks which enable any plugin to add or modify certain parts of the output and at certain stages in the rendering process.
There are cases where we want any plugin to contribute to a chunk of the output of any given page. We want this to be loosely coupled so you don't have say an admin tool's code leaking into your theme. In the past many plugins had an installation step like "Copy this into your theme header" which is what we want to completely avoid.


There are a variety of [[Callbacks]] which enable any plugin to add or modify certain parts of the output and at certain stages in the rendering process. These callbacks are probably most useful for local plugins or admin tools which bring some functionality to the whole site instead of just certain pages. But they can also be used by any plugin to conditionally augment the output too.
= add_htmlattributes =
= add_htmlattributes =
{{Moodle 3.3}}


This is used to append extra attributes into the html element of the page which are required elsewhere. An example might be a facebook block plugin which uses the opengraph js libraries which need the xml namespace to be setup. It should return an array of attribute key and values like this:
This is used to append extra attributes into the html element of the page which are required elsewhere. An example might be a facebook block plugin which uses the opengraph js libraries which need the xml namespace to be setup. It should return an array of attribute key and values like this:
 
<syntaxhighlight lang="php">
<code>
function tool_facebook_add_htmlattributes() {
function tool_facebook_add_htmlattributes() {
     return array(
     return array(
         'xmlns:og' => 'http://ogp.me/ns#', # this should be in the page
         'xmlns:og' => 'http://ogp.me/ns#',
     );
     );
}
}
</code>
</syntaxhighlight>
 
Because multiple plugins can add extra attributes there is potential namespace clash issues. ie in the Facebook opengraph example stick with "xmlns:og" as specified by Facebook so that if multiple plugins declare the same key / value then they will match. Not that duplicates attribute keys are merged.
 
= before_footer =
= before_footer =
{{Moodle 3.3}}


This enables you to easily inject a chunk of JS or CSS into every page, for instance an analytics tool like Google Analytics or Facebook pixel. If a string is returned, it will be added to the page, as described in https://tracker.moodle.org/browse/MDL-68564
<syntaxhighlight lang="php">
function tool_mytool_before_footer() {
    global $PAGE;
  $PAGE->requires->js_init_code("alert('before_footer');");
}
</syntaxhighlight>
= before_http_headers =
= before_http_headers =
{{Moodle 3.3}}
This enables you to easily inject a HTTP header into every page. It only has side effects and it's return value is ignored:
<syntaxhighlight lang="php">
function tool_headertest_before_http_headers() {
    header("X-CustomHeader: SomeValue");
}
</syntaxhighlight>
Note that this is called after the page is generated and just prior to the headers being sent. So internal system state may have already been changed (eg adding events to the log) and the page generation may have been expensive and the student will be considered to have viewed the page.


You should generally not use this callback for things like redirecting away under some conditions, instead consider either the [[Login_callbacks#after_config|after_config]] or the [[Login_callbacks#after_require_login|after_require_login]] callbacks which fire earlier in the request lifecycle.
= before_standard_html_head =
= before_standard_html_head =
{{Moodle 3.3}}


An better API alternative to appending to $CFG->additionalhtmlhead
This is an API alternative to appending to $CFG->additionalhtmlhead and could be used for adding meta tags or similar to the page. It MUST return a string containing a well formed html chunk, or at minimum an empty string.
 
<syntaxhighlight lang="php">
function tool_headtag_before_standard_html_head() {
    return "<meta name='foo' value='before_top_of_body_html' />\n";
}
</syntaxhighlight>
= before_standard_top_of_body_html =
= before_standard_top_of_body_html =
{{Moodle 3.3}}


This enables a plugin to insert a chunk of html at the start of the html document. Typical use cases include some sort of alert notification, but in many cases the [[Notifications]] may be a better fit. It MUST return a string containing a well formed chunk of html, or at minimum an empty string.
<syntaxhighlight lang="php">
function tool_callbacktest_before_standard_top_of_body_html() {
    return "<div style='background: red'>Before standard top of body html</div>";
}
</syntaxhighlight>
= render_navbar_output =
= render_navbar_output =
{{Moodle 3.2}}
TBA

Latest revision as of 10:00, 27 March 2023

There are cases where we want any plugin to contribute to a chunk of the output of any given page. We want this to be loosely coupled so you don't have say an admin tool's code leaking into your theme. In the past many plugins had an installation step like "Copy this into your theme header" which is what we want to completely avoid.

There are a variety of Callbacks which enable any plugin to add or modify certain parts of the output and at certain stages in the rendering process. These callbacks are probably most useful for local plugins or admin tools which bring some functionality to the whole site instead of just certain pages. But they can also be used by any plugin to conditionally augment the output too.

add_htmlattributes

Moodle 3.3


This is used to append extra attributes into the html element of the page which are required elsewhere. An example might be a facebook block plugin which uses the opengraph js libraries which need the xml namespace to be setup. It should return an array of attribute key and values like this:

function tool_facebook_add_htmlattributes() {
     return array(
         'xmlns:og' => 'http://ogp.me/ns#',
     );
}

Because multiple plugins can add extra attributes there is potential namespace clash issues. ie in the Facebook opengraph example stick with "xmlns:og" as specified by Facebook so that if multiple plugins declare the same key / value then they will match. Not that duplicates attribute keys are merged.

before_footer

Moodle 3.3


This enables you to easily inject a chunk of JS or CSS into every page, for instance an analytics tool like Google Analytics or Facebook pixel. If a string is returned, it will be added to the page, as described in https://tracker.moodle.org/browse/MDL-68564

function tool_mytool_before_footer() {
    global $PAGE;
   $PAGE->requires->js_init_code("alert('before_footer');");
}

before_http_headers

Moodle 3.3


This enables you to easily inject a HTTP header into every page. It only has side effects and it's return value is ignored:

function tool_headertest_before_http_headers() {
    header("X-CustomHeader: SomeValue");
}

Note that this is called after the page is generated and just prior to the headers being sent. So internal system state may have already been changed (eg adding events to the log) and the page generation may have been expensive and the student will be considered to have viewed the page.

You should generally not use this callback for things like redirecting away under some conditions, instead consider either the after_config or the after_require_login callbacks which fire earlier in the request lifecycle.

before_standard_html_head

Moodle 3.3


This is an API alternative to appending to $CFG->additionalhtmlhead and could be used for adding meta tags or similar to the page. It MUST return a string containing a well formed html chunk, or at minimum an empty string.

function tool_headtag_before_standard_html_head() {
    return "<meta name='foo' value='before_top_of_body_html' />\n";
}

before_standard_top_of_body_html

Moodle 3.3


This enables a plugin to insert a chunk of html at the start of the html document. Typical use cases include some sort of alert notification, but in many cases the Notifications may be a better fit. It MUST return a string containing a well formed chunk of html, or at minimum an empty string.

function tool_callbacktest_before_standard_top_of_body_html() {
    return "<div style='background: red'>Before standard top of body html</div>";
}

render_navbar_output

Moodle 3.2


TBA