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


From MoodleDocs
Revision as of 12:31, 14 October 2020 by Florian Metzger-Noel (talk | contribs) (Removed \core\notification::problem($message); because it doesn't work - the function doesn't exist in current moodle)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Moodle 3.1

New APIs were created around notifications in Moodle 3.1. These allow for notifications to be added to the session and subsequently retrieved, either during the current page load, from within the page, or on a later page.

NOTE: Not to be confused with sending an arbitrary user either a 1-1 message, a group message, or a system message which are also called "Notifications", please see the Message API.

Adding notifications

The API allows for creation of four types of notification: error, warning, info, and success.

Redirect notifications

The typical way of using these notifications is when calling the redirect() function. This takes arguments for the target URL, as well as the notification message, and the notification type.

This will likely cover a majority of uses in Moodle.

   redirect('/index.php', 'Message on redirect', null, \core\output\notification::NOTIFY_SUCCESS);

Adding other notifications

For those wishing to add notifications outside of a redirect, then the following methods exist for adding notifications:

   // Add a notification of some kind.
   \core\notification::add($message, $type);
   // Or use the following helper functions:

Displaying in-page notifications

It is also possible to display standard notifications from JavaScript after the page has loaded.

This can be achieved using the core/notification AMD module's addNotificiation(), or addNotifications functions.

Notifications added in this way are not added to the session. They are only displayed immediately on the page.

   require(['core/notification'], function(notification) {
       message: "Your message here",
       type: "info"
         message: "Your notification here",
         type: "info"
         message: "Your second notification here",
         type: "success"

Rendering notifications

Notifications are usually rendered automatically at the following times:

  • If notifications are added after the page heading, but before the footer, these are immediately output;
  • If notifications are added after the page footer, these are stored in the session until fetched at a later time; and
  • If notifications are added before the page heading starts, they are automatically fetched and rendered during render of the page heading (e.g. $OUTPUT->heading()). This includes those stored in the session from a previous page load.

Notifications can also be fetched using the core/notification AMD module's fetchNotifications() function.

This is called as standard by a web service shortly after page load.