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...")
 
Line 15: Line 15:


= before_footer =
= before_footer =
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. It only has side effects and it's return value is ignored:
<code>
function tool_mytool_before_footer() {
    global $PAGE;
  $PAGE->requires->js_init_code("alert('before_footer');");
}
</code>


= before_http_headers =
= before_http_headers =

Revision as of 02:08, 22 May 2017

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.

add_htmlattributes

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#', # this should be in the page
    );

}


before_footer

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. It only has side effects and it's return value is ignored:

function tool_mytool_before_footer() {

   global $PAGE;
  $PAGE->requires->js_init_code("alert('before_footer');");

}

before_http_headers

before_standard_html_head

An better API alternative to appending to $CFG->additionalhtmlhead

before_standard_top_of_body_html

render_navbar_output