Notices / $notices

$notices is the API variable that holds all notices aka notifications (messages, warnings, and errors) generated during a request

Notices are primarily used in the admin to display feedback to the user, but can also be iterated and rendered in front-end templates.

Every class that extends Wire (modules, Fieldtypes, Inputfields, Processes, and more) inherits message(), warning(), and error() methods that add notices to $notices automatically.

$notices is accessible in templates as $notices or wire()->notices; and in modules as $this->wire()->notices.

Adding notices

Notices are most commonly added from within a Wire-derived class via the inherited message(), warning(), and error() methods. These are available on every module, Fieldtype, Inputfield, Process, and other Wire-derived class.

// In a module or any Wire subclass:

$this->message("Item was saved successfully");
$this->warning("Setting X is deprecated, use Y instead");
$this->error("Could not connect to the API");

Each method accepts an optional $flags argument — see the Notice flags section below.

// Also log the notice to messages.txt / errors.txt
$this->message("Item saved", Notice::log);
$this->error("Something failed", Notice::log);

// Only log, don't display
$this->message("Background task ran", Notice::logOnly);

// Only show in debug mode
$this->warning("Slow query detected", Notice::debug);

// Allow HTML markup in the notice text
$this->message("See the <a href='/docs/'>docs</a>", Notice::allowMarkup);

// Combine flags with bitmask OR or a space-separated string 3.0.149+
$this->message("Done", Notice::log | Notice::noGroup);
$this->message("Done", "log noGroup");

// Boolean true is a shortcut for Notice::log
$this->message("Item saved", true);

Notices can also be added directly to the $notices API variable from a template using the same methods, since $notices itself extends Wire:

$notices->message("Hello from a template");
$notices->error("Something went wrong");

Or by constructing a Notice object directly:

$notices->add(new NoticeMessage("Hello world"));
$notices->add(new NoticeError("Something failed", Notice::log | Notice::noGroup));

Passing arrays and objects

Notice methods accept arrays and objects in place of a string, which is useful for debugging. The value is formatted via Debug::toStr() and Notice::allowMarkup is set automatically.

// Display a formatted dump of any array or object
$this->message($someArray);
$this->message($somePage);

Pass a single-key associative array to add a label before the formatted value. The key becomes a bold "Label: value" prefix in the notice:

$this->message(['Returned pages' => $pages->find('template=blog')]);
$this->message(['Order data' => $orderArray]);

In debug mode ($config->debug = true), the label is used as the notice's class attribution (shown as the source) rather than a visible prefix.

Notice flags

Flags control how and when a notice is displayed or logged. They can be combined as a bitmask using |, or specified as a space-separated string of flag names 3.0.149+.

// These are all equivalent:
$this->message("Text", Notice::log | Notice::noGroup);
$this->message("Text", "log noGroup");
$this->message("Text", Notice::log | Notice::noGroup | Notice::superuser);
$this->message("Text", "log noGroup superuser");
FlagString nameDescription
Notice::loglogAlso write this notice to the corresponding log file
Notice::logOnlylogOnlyWrite to log only — do not display to the user
Notice::debugdebugOnly show when $config->debug is true
Notice::allowMarkupallowMarkupAllow HTML in the notice text (not entity-encoded)
Notice::allowMarkdownallowMarkdownParse basic inline Markdown and bracket markup
Notice::noGroupnoGroupDo not group/collapse with other notices of the same type
Notice::allowDuplicateallowDuplicateShow duplicate notices separately rather than collapsing
Notice::prependprependAdd to the top of the notice list rather than the bottom
Notice::anonymousanonymousDo not associate notice with the calling class
Notice::loginloginOnly show to logged-in users
Notice::adminadminOnly show when the current page is in the admin
Notice::superusersuperuserOnly show to superusers

Aliases (same value, different name):

AliasSame as
Notice::markupNotice::allowMarkup
Notice::markdownNotice::allowMarkdown
Notice::separateNotice::noGroup
Notice::duplicateNotice::allowDuplicate
Reading and iterating notices

$notices extends WireArray, so it can be iterated directly. Each item is a Notice object — specifically a NoticeMessage, NoticeWarning, or NoticeError instance.

foreach($notices as $notice) {
    // skip debug notices unless debug mode is on...
    if($notice->flags & Notice::debug && !$config->debug) continue;
    // ...or use if(!$notice->viewable()) continue;

    // entity-encode unless markup is allowed
    $text = ($notice->flags & Notice::allowMarkup)
        ? $notice->text
        : $sanitizer->entities($notice->text);

    if($notice instanceof NoticeError) {
        echo "<p class='error'>$text</p>";
    } else if($notice instanceof NoticeWarning) {
        echo "<p class='warning'>$text</p>";
    } else {
        echo "<p class='message'>$text</p>";
    }
}

$notices->hasErrors()

Returns true if any NoticeError items are present.

if($notices->hasErrors()) {
    echo "There were errors — please review.";
}

$notices->hasWarnings()

Returns true if any NoticeWarning items are present.

$notices->getVisible()

Returns a new Notices object containing only notices that are visible to the current user (respects login, admin, superuser, debug, and logOnly flags). Available 3.0.252.

$visible = $notices->getVisible();
foreach($visible as $notice) {
    echo $notice->text . "\n";
}

$notices->add($notice)

Add a NoticeMessage, NoticeWarning, or NoticeError object directly. Duplicate notices are collapsed by default and the retained notice's qty property is incremented. Use Notice::allowDuplicate to keep duplicate notices as separate items.

  • Arguments: add(Notice $notice)
  • Returns: $notices
$notices->add(new NoticeMessage("Saved"));
$notices->add(new NoticeWarning("Review this setting", "noGroup"));
$notices->add(new NoticeError("Something failed", Notice::allowDuplicate));
Rendering notices

$notices->render()

Render notices as HTML using the admin theme's standard notice markup. Useful for displaying notices outside of the normal admin flow. Available 3.0.254.

  • Returns: string — HTML output
  • This is a hookable method (___render()).
echo $notices->render();

$notices->renderText()

Render notices as plain text. If outputting inside HTML (e.g. a <pre>), pass the result through $sanitizer->entities() first. For CLI the return value can be output directly. Available 3.0.254.

  • Returns: string
  • This is a hookable method (___renderText()).
$text = $notices->renderText();
echo '<pre>' . $sanitizer->entities($text) . '</pre>';
Notice properties

Each Notice object exposes the following properties:

PropertyTypeDescription
textstringThe notice message text
flagsintBitmask of active Notice::* flags
flagsStrstringActive flags as a space-separated string (read-only)
flagsArrayarrayActive flags as [int => name] array (read-only)
classstringClass name of the object that added the notice
iconstringIcon name (FontAwesome name without fa- prefix)
timestampintUnix timestamp of when the notice was created
qtyintNumber of times this notice was added (duplicates collapsed)
idStrstringStable ID string derived from type, flags, class, and text

Notice flag helpers

These methods are available on each Notice object.

MethodDescription
$notice->flags()Get the integer flag bitmask
$notice->flags($flags)Replace flags using an integer, array of names, or space/comma-separated names
$notice->addFlag($flag)Add one flag by integer or name
$notice->removeFlag($flag)Remove one flag by integer or name
$notice->hasFlag($flag)Return true when a flag is present
$notice->getName()Return the related log name: messages, warnings, or errors
$notice->getIdStr()Return the same value exposed by the read-only idStr property
$notice->viewable()Return whether this notice is viewable to the current user/request
$notice = new NoticeMessage("Saved", "log noGroup");

if($notice->hasFlag('log')) {
    $notice->removeFlag('log');
}

$notice->addFlag('allowMarkup');
echo $notice->flagsStr; // "noGroup allowMarkup"
Notes
  • Source files: wire/core/Notices/Notice.php and wire/core/Notices/Notices.php.
  • The Notice::log flag writes to the log named after the notice type: NoticeMessagemessages.txt, NoticeWarningwarnings.txt, NoticeErrorerrors.txt.
  • Notice::allowMarkdown automatically converts the text using $sanitizer->entitiesMarkdown() and then sets allowMarkup — the original allowMarkdown flag is removed before display.
  • Duplicate notices (same type, text, and flags) are silently collapsed by default. Use Notice::allowDuplicate to prevent collapsing.
  • An icon can be set by prefixing the message text with icon-{name} (space after): $this->message("icon-check Item saved") sets the icon to check.
  • The $notices API variable is shared across all Wire objects in the request. Notices added by any module or class accumulate in the same collection.
API reference: methods, hooks

This class manages notices that have been sent by Wire::message(), Wire::warning() and Wire::error() calls. The message(), warning() and error() methods are available on every Wire derived object. This class is primarily for internal use in the admin. However, it may also be useful in some front-end contexts.

// Adding a NoticeMessage using object syntax
$notices->add(new NoticeMessage("Hello World"));

// Adding a NoticeMessage using regular syntax
$notices->message("Hello World");

// Adding a NoticeWarning, and allow markup in it
$notices->message("Hello <strong>World</strong>", Notice::allowMarkup);

// Adding a NoticeError that only appears if debug mode is on
$notices->error("Hello World", Notice::debug); 

Iterating and outputting Notices:
In ProcessWire 3.0.254+ you can use the $notices->render() or $notices->renderText() methods. In all versions of ProcessWire, you can also output them manually like this:

foreach($notices as $notice) {
  // skip over debug notices, if debug mode isn't active
  if($notice->flags & Notice::debug && !$config->debug) continue;
  // entity encode notices unless the allowMarkup flag is set
  if($notice->flags & Notice::allowMarkup) {
    $text = $notice->text;
  } else {
    $text = $sanitizer->entities($notice->text);
  }
  // output either an error, warning or message notice
  if($notice instanceof NoticeError) {
    echo "<p class='error'>$text</p>";
  } else if($notice instanceof NoticeWarning) {
    echo "<p class='warning'>$text</p>";
  } else {
    echo "<p class='message'>$text</p>";
  }
}

Click any linked item for full usage details and examples. Hookable methods are indicated with the icon. In addition to those shown below, the Notices class also inherits all the methods and properties of: WireArray and Wire.

Show $var?     Show args?       Only hookable?    

Additional methods and properties

In addition to the methods and properties above, Notices also inherits the methods and properties of these classes:

API reference based on ProcessWire core version 3.0.264