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, properties, constants

Base class that holds a message, source class, and timestamp. Contains individual notices/messages used by the application to display to the user. Notice items come in three different classes: NoticeMessage, NoticeWarning and NoticeError. They are all identical in terms of API, with the only difference being that they render as informational messages, warnings, or errors.


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

Show class?     Show args?       Only hookable?    

Constants

NameReturnSummary 
Notice::admin const524288Ignore notice unless user is somewhere in the admin (login page included) @3.0.149  
Notice::allowDuplicate const8388608Present duplicate notices separately rather than collapsing them to one String name can be referred to as 'allowDuplicate' or just 'duplicate' @3.0.208  
Notice::allowMarkdown const4194304Allow parsing of basic/inline markdown and bracket markup per $sanitizer->entitiesMarkdown() @3.0.165  
Notice::allowMarkup const32Flag indicates the notice is allowed to contain markup and won’t be automatically entity encoded Note: entity encoding is done by the admin theme at output time, which should detect this flag. 
Notice::anonymous const65536Make notice anonymous (not tied to a particular class) @3.0.135  
Notice::debug const2Flag indicates the notice is for when debug mode is on only 
Notice::duplicate const8388608Alias of allowDuplicate flag @3.0.208  
Notice::log const8Flag indicates the notice will also be sent to the messages or errors log 
Notice::logOnly const16Flag indicates the notice will be logged, but not shown 
Notice::login const262144Ignore notice unless it will be seen by a logged-in user @3.0.149  
Notice::markdown const4194304Alias of allowMarkdown flag @3.0.208  
Notice::markup const32Alias of allowMarkup flag @3.0.208  
Notice::noGroup const131072Indicate notice should not group/collapse with others of the same type (when supported by admin theme) @3.0.146  
Notice::prepend const1Flag indicates notice should prepend (rather than append) to any existing notices @3.0.135  
Notice::separate const131072Alias of noGroup flag @3.0.208  
Notice::superuser const1048576Ignore notice unless current user is a superuser @3.0.149  

Properties

NameReturnSummary 
Notice::class string Class of notice 
Notice::icon string Name of icon to use with Notice 
Notice::idStr string Unique ID string for Notice 
Notice::qty int Number of times this Notice was added. 
Notice::text string object array Text or value of notice 
Notice::timestamp int Unix timestamp of when the notice was generated 

Additional methods and properties

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

API reference based on ProcessWire core version 3.0.263