Difference between revisions of "Notifications"

Jump to: navigation, search
m (Fixed spelling of "notification" in sample JS code for displaying notifications.)
(Adding success API)
Line 26: Line 26:
 
     \core\notification::info($message);
 
     \core\notification::info($message);
 
     \core\notification::problem($message);
 
     \core\notification::problem($message);
 +
    \core\notification::success($message);
  
 
=== Displaying in-page notifications ===
 
=== Displaying in-page notifications ===

Revision as of 00:09, 26 February 2019

Notifications

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.

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:
   \core\notification::error($message);
   \core\notification::warning($message);
   \core\notification::info($message);
   \core\notification::problem($message);
    \core\notification::success($message);

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) {
     notification.addNotification({
       message: "Your message here",
       type: "info"
     });
     notification.addNotifications([
       {
         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.