Notifications

Notifications API allow for in-page notifications to be added to the session and subsequently retrieved, either during the current page load or on the next page.

Types of notifications

The API allows for creation of four types of notification: error, warning, info, and success, represented by the following constants:

// A notification of level 'success'.
\core\notification\SUCCESS = 'success';

// A notification of level 'warning'.
\core\notification\WARNING = 'warning';

// A notification of level 'info'.
\core\notification\INFO = 'info';

// A notification of level 'error'.
\core\notification\ERROR = 'error';


Adding notifications

The typical way of adding notifications is combined with the use of redirect().

Combination of helper functions with redirect() - currently preferred approach

\core\notification::error($message);
\core\notification::warning($message);
\core\notification::info($message);
\core\notification::success($message);
redirect('URL.php');

To add a notification of any kind, use:

\core\notification::add($message, $type); \\ $type is one of the \core\notification\[SUCCESS|WARNING|INFO|ERROR] constants 
redirect('URL.php');

Directly in redirect()

redirect('/url.php', 'Notification message', null, \core\notification::SUCCESS);

Legacy notifications mapping and conversion

Prior to Totara 13, notifications were added to the queue by calling totara_set_notification($message, $url, array('class'=>'notifysuccess')). All use of  totara_set_notification has now been  replaced by the core Notification API.

To convert your code to the new approach, use the following CSS class mapping guidelines:

'notifyproblem'     => self::NOTIFY_ERROR,
'notifytiny'        => self::NOTIFY_ERROR,
'notifyerror'       => self::NOTIFY_ERROR,
'notifysuccess'     => self::NOTIFY_SUCCESS,
'notifymessage'     => self::NOTIFY_INFO,
'notifyredirect'    => self::NOTIFY_INFO,
'redirectmessage'   => self::NOTIFY_INFO,

Example 1:

// Legacy code with redirect
    totara_set_notification($message, $url, array('class' => 'notifysuccess'))
// converts into
    \core\notification::success($message);
    redirect($url);

Example 2:

// Legacy code without redirect
    totara_set_notification($message, null, array('class' => 'notifymessage'))
// converts into
    \core\notification::info($message);