Note: This site is no longer used and is in read-only mode. Instead please go to our newMoodle Developer Resource site. |
The Page API is used to set up the current page, add JavaScript, and configure how things will be displayed to the user.
The Page API is an integral part of any Moodle page. It allows the developer to set things up the way they envisage it. Through the Page API you can set things like the title, initial heading, where the user is for the navigation, and which layout you think the page should use.
This document starts off with a simple example, and then proceeds to provide a more complete description of how to set up a page for display.
This example covers how to set up a basic page for use within an activity plugin and is undoubtedly the simplest example as much of the work is done behind the scenes for you.
// File: /mod/mymodulename/view.phprequire_once('../../config.php');$cmid=required_param('id',PARAM_INT);$cm=get_coursemodule_from_id('mymodulename',$cmid,0,false,MUST_EXIST);$course=$DB->get_record('course',array('id'=>$cm->course),'*',MUST_EXIST);require_login($course,true,$cm);$PAGE->set_url('/mod/mymodulename/view.php',array('id'=>$cm->id));$PAGE->set_title('My modules page title');$PAGE->set_heading('My modules page heading');// The rest of your code goes below this.
If you are not familiar with what the first four lines are doing try going through these docs before proceeding -
So lets start at require_login and assume you already have the course and course module objects ready to use. When you call require_login part of the magic it does for you is set up the basic for the current page.
In the case of the example above because require_login is given a course and course module it is already setting up much of the page for you. It is giving the course and course module objects to the page, setting the context for the page to the course modules context, and setting the page layout toincourse so that you get the standard look of a course module.
The set up that we are having to do is as follows:
It's important to mention that this has to be done before output starts. That means you must set up the page before the header is printed and before you instantiate any moodleform instances.
And that is it, if you were to add a bit of simple output there you would get a page that already looks like other module pages you would have seen. Simple as.
For every page request Moodle sets up a couple of global structures that you will likely need. $DB the database object, and $CFG which stores configuration are two that you are likely already aware of. $PAGE is the focus of this article, it is a moodle_page instance that stores all of the information and is used by the output library $OUTPUT when displaying the page.
It's important to note the difference between $PAGE and $OUTPUT, $PAGE is for setting up the page and $OUTPUT is for displaying the page. $PAGE contains lots of logic and magic, $OUTPUT is purely about display and does little more than produce HTML.
When creating a page in Moodle there are a couple of things that you must set, and a couple of things that get set for you in many cases but not all of the time.
This is an absolute must, failing to set this will lead Moodle to display an error that it has not been set.
It can be set in the following manner:
$PAGE->set_url(newmoodle_url('/path/to/your/file.php',array('key'=>'value','id'=>3)));$PAGE->set_url('/path/to/your/file.php',array('key'=>'value','id'=>3));$PAGE->set_url('/path/to/your/file.php?key=value&id=3');
The above code sets the page URL 3 times, and highlights the 3 different ways you can set the URL. Either of the first two methods are the preferred way as it provides 100% accuracy when processing the URL. Internally set_url() converts what ever you give it to a moodle_url object.
The URL that you give to the page is going to be used by many Moodle core API's. Most importantly it is going to be used to create the navigation for your page so it's very important you set it accurately.
This is an absolute must as well, however in many cases it will be set for you magically by Moodle.
In order to set the context for the page you must provide a context object, in Moodle 2.2 and greater this will look as follows:
// Moodle 2.2 and greater$PAGE->set_context(context_system::instance());$PAGE->set_context(context_coursecat::instance($categoryid));$PAGE->set_context(context_course::instance($courseid));$PAGE->set_context(context_module::instance($moduleid));
In Moodle 2.0+, and Moodle 2.1+ the following is the equivalent code:
// Moodle 2.0 and 2.1$PAGE->set_context(get_system_context());$PAGE->set_context(get_context_instance(CONTEXT_COURSECAT,$categoryid));$PAGE->set_context(get_context_instance(CONTEXT_COURSE,$courseid));$PAGE->set_context(get_context_instance(CONTEXT_MODULE,$moduleid));
In both examples above setting different types of contexts has been illustrated however you should only ever call set_context() once with the context that is most appropriate to the page you are creating.
If it is a plugin then the context to use would be the context you are using for your capability checks.
As mentioned above the other thing to be aware of is that in some circumstances this gets automatically set for you.
If your script calls require_login (and most scripts have to) and you are providing a course, or a module to your require login call then you will not need to call set_context().
This is because require_login handles it for you.
If your script doesn't call require_login, or you don't call it with a course and/or module then you will need to manually set the context as shown.
The following are optional extras you can set up against the PAGE object that you are likely to encounter throughout Moodle core, and are likely to want to use yourself.
The following code sets the pages layout to the standard layout, the most generic layout in the arsenal.
$PAGE->set_pagelayout('standard');
When setting the page layout you should use the layout that is the closest match to the page you are creating. Layouts are used by themes to determine what is shown on the page. The most prominent difference between layouts is the block regions they support. The default layout `base` for example doesn't normally have any block regions at all, where as normally `standard` has the most generic layout and several block regions.
There are dozens of different layouts that can be, and are used throughout Moodle core that you can use within your code. For a full list of common layouts you are best too look at theme/base/config.php or refer to the list below.
Note: It's important to know that the theme determines what layouts are available and how each looks. If you select a layout that the theme doesn't support then it will revert to the default layout while using that theme.
Themes are also able to specify additional layouts, however its important to spot them and know that while they may work with one theme they are unlikely to work as you expect with other themes.
The following is a list of the layouts defined by the base theme. Theme designers are encouraged to make the base theme a parent of their custom theme so you can be sure that in 99% of cases these layouts will be available.
Layout | Description |
---|---|
base | Most backwards compatible layout without the blocks. This is the layout used by default. |
standard | Standard layout with blocks, this is recommended for most pages with general information |
course | The course main page uses this layout. |
coursecategory | Category course listings. |
incourse | Used for areas within a course, typical for modules. Default page layout if $cm specified in require_login(). |
frontpage | The site home page uses this. |
admin | Admin and settings pages as well as server administration scripts. |
mydashboard | The users dashboard. |
mypublic | A users public profile uses this layout. |
login | The login screen. |
popup | Pages that appear in popup windows, usually no navigation, blocks, or header. |
frametop | Used for the outermost content of a page constructed with frames. Usually no blocks and minimal footer. |
embedded | Embedded pages such as content for iframes/objects. Needs as much space as possible usually no blocks, header, or footer. |
maintenance | Used during upgrade, installation, and when maintenance mode is enabled. |
Gets used when printing a page. Normally just a simple header and no blocks. | |
redirect | A special layout used during a redirect. Normally with content only. |
report | Used for reports within Moodle. Special layout designed to handle horizontal scrolling in a nice way. |
Setting an appropriate title is certainly a must for any properly designed page. While it is optional it is highly recommended that you set the title.
$PAGE->set_title('This is my title');
When setting the title for the page you need to provide just the string you want to use for the title. It should be a basic string and contain no HTML. Any HTML will be stripped out as the title is used within the <title> tag in the HTML head.
Like title it is highly recommended that you set a meaningful heading for the page, although it is optional.
The heading is normally displayed at the top of the page before the rest of the content starts. However it is up to the layout defined by the theme as to where it is displayed. Not all layouts will display a heading but I encourage you to always set one even if you are using a layout that doesn't support headings. This way if you are using a theme that uses a heading on every page regardless of layout things still look consistent.
$PAGE->set_heading(get_string('pluginname','local_myplugin'));
When setting a heading there is just one argument, the string to use for the heading. It should be a basic string and contain no HTML.
The following are advanced optional methods you can call to further set up your page. In most cases you will never need to use these.
$PAGE->set_activity_record($activityrecord);
$PAGE->set_blocks_editing_capability($strcapability);
$PAGE->set_button($htmlstring);
$PAGE->set_cacheable(true/false);
$PAGE->set_category_by_id($categoryid);
$PAGE->set_cm($coursemodulerecord);
$PAGE->set_course($courserecord);
$PAGE->set_docs_path($strpath);
$PAGE->set_focuscontrol($controlid);
$PAGE->set_headingmenu($htmlstring);
$PAGE->set_other_editing_capability($strcapability);
$PAGE->set_pagetype($strpagetype);
$PAGE->set_periodic_refresh_delay($intdelay);
$PAGE->set_popup_notification_allowed(true/false);
$PAGE->set_subpage($strsubpage);
$PAGE->add_body_class($strcssclass);
$PAGE->add_body_classes($arrayofclasses);
$PAGE->force_settings_menu();
As well of setting up the page you can of course get information back from it about the page it has been set up to display.
Anything you set against the page can be retrieved as can any information that was set magically for you by other methods.
The following are the most interesting and likely useful things you can get back from the page.
$var=$PAGE->activityrecord;
$var=$PAGE->blockmanager;
$var=$PAGE->bodyid;
$var=$PAGE->categories;
$var=$PAGE->category;
$var=$PAGE->cm;
$var=$PAGE->course;
$var=$PAGE->devicetypeinuse;
$var=$PAGE->headerprinted;
$var=$PAGE->heading;
$var=$PAGE->navbar;
$var=$PAGE->navigation;
$var=$PAGE->requires;
$var=$PAGE->settingsnav;
$var=$PAGE->theme;
$var=$PAGE->title;
$var=$PAGE->url;
There are a couple of API's that are closely related to the Page API that you should be aware of as well.
The output API is an immediate relation of the page API. The page API is about setting things up, whereas the output API is all about displaying things.It's through the output API that content is actually produced, and much of the information you set up through the page is used to customise what is produced, and fill in the general blanks of any page (such as title and heading.
See theOutput API documentation for more information.
The page requirements API allows you the developer to include additional CSS, and JavaScript resources that should be included with the page, and to include JavaScript calls within the page through a variety of means.Technically this API is part of the Output API mentioned above, however it deserves special mention. If you are going to be using any JavaScript or CSS within your page you will need to know about this.
See theOutput API documentation for more information on the page requirements API.
The final API to mention is the navigation API. This again is integral to both the page and output API and is used to recognise the context of the content being displayed and ensure that the correct blocks and navigaiton structure are loaded for the context.There is a good chance that you will encounter a need to customise the navigation early on in plugin page development and it's important to be aware of this important API.
See theNavigation API documentation for more information on the page requirements API.