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.
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.
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");| Flag | String name | Description |
|---|---|---|
Notice::log | log | Also write this notice to the corresponding log file |
Notice::logOnly | logOnly | Write to log only — do not display to the user |
Notice::debug | debug | Only show when $config->debug is true |
Notice::allowMarkup | allowMarkup | Allow HTML in the notice text (not entity-encoded) |
Notice::allowMarkdown | allowMarkdown | Parse basic inline Markdown and bracket markup |
Notice::noGroup | noGroup | Do not group/collapse with other notices of the same type |
Notice::allowDuplicate | allowDuplicate | Show duplicate notices separately rather than collapsing |
Notice::prepend | prepend | Add to the top of the notice list rather than the bottom |
Notice::anonymous | anonymous | Do not associate notice with the calling class |
Notice::login | login | Only show to logged-in users |
Notice::admin | admin | Only show when the current page is in the admin |
Notice::superuser | superuser | Only show to superusers |
Aliases (same value, different name):
| Alias | Same as |
|---|---|
Notice::markup | Notice::allowMarkup |
Notice::markdown | Notice::allowMarkdown |
Notice::separate | Notice::noGroup |
Notice::duplicate | Notice::allowDuplicate |
$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));$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>';Each Notice object exposes the following properties:
| Property | Type | Description |
|---|---|---|
text | string | The notice message text |
flags | int | Bitmask of active Notice::* flags |
flagsStr | string | Active flags as a space-separated string (read-only) |
flagsArray | array | Active flags as [int => name] array (read-only) |
class | string | Class name of the object that added the notice |
icon | string | Icon name (FontAwesome name without fa- prefix) |
timestamp | int | Unix timestamp of when the notice was created |
qty | int | Number of times this notice was added (duplicates collapsed) |
idStr | string | Stable ID string derived from type, flags, class, and text |
Notice flag helpers
These methods are available on each Notice object.
| Method | Description |
|---|---|
$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"- Source files:
wire/core/Notices/Notice.phpandwire/core/Notices/Notices.php. - The
Notice::logflag writes to the log named after the notice type:NoticeMessage→messages.txt,NoticeWarning→warnings.txt,NoticeError→errors.txt. Notice::allowMarkdownautomatically converts the text using$sanitizer->entitiesMarkdown()and then setsallowMarkup— the originalallowMarkdownflag is removed before display.- Duplicate notices (same type, text, and flags) are silently collapsed by default.
Use
Notice::allowDuplicateto 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 tocheck. - The
$noticesAPI variable is shared across all Wire objects in the request. Notices added by any module or class accumulate in the same collection.
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.
Common
| Name | Return | Summary | |
|---|---|---|---|
Notice::__construct() Notice::__construct(string $text) Notice::__construct(string $text, $flags = 0) | None | Create the Notice | |
Notice::addFlag() Notice::addFlag($flag) Notice::addFlag($flag) | None | Add a flag | |
Notice::flags() Notice::flags() Notice::flags($value = null) | int | Get or set flags Can also be used as property: Notice::flags | |
Notice::get() Notice::get(string $key) Notice::get(string $key) | mixed | Get property | |
Notice::getName() Notice::getName() Notice::getName() | string | Get the name for this type of Notice | |
Notice::hasFlag() Notice::hasFlag($flag) Notice::hasFlag($flag) | bool | Does this Notice have given flag? | |
Notice::removeFlag() Notice::removeFlag($flag) Notice::removeFlag($flag) | None | Remove a flag | |
Notice::set() Notice::set(string $key, mixed $value) Notice::set(string $key, mixed $value) | this WireData | Set property | |
Notice::viewable() Notice::viewable() Notice::viewable() | bool | Is this notice viewable at runtime? |
Constants
| Name | Return | Summary | |
|---|---|---|---|
| Notice::admin const | 524288 | Ignore notice unless user is somewhere in the admin (login page included) @3.0.149 | |
| Notice::allowDuplicate const | 8388608 | Present 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 const | 4194304 | Allow parsing of basic/inline markdown and bracket markup per $sanitizer->entitiesMarkdown() @3.0.165 | |
| Notice::allowMarkup const | 32 | Flag 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 const | 65536 | Make notice anonymous (not tied to a particular class) @3.0.135 | |
| Notice::debug const | 2 | Flag indicates the notice is for when debug mode is on only | |
| Notice::duplicate const | 8388608 | Alias of allowDuplicate flag @3.0.208 | |
| Notice::log const | 8 | Flag indicates the notice will also be sent to the messages or errors log | |
| Notice::logOnly const | 16 | Flag indicates the notice will be logged, but not shown | |
| Notice::login const | 262144 | Ignore notice unless it will be seen by a logged-in user @3.0.149 | |
| Notice::markdown const | 4194304 | Alias of allowMarkdown flag @3.0.208 | |
| Notice::markup const | 32 | Alias of allowMarkup flag @3.0.208 | |
| Notice::noGroup const | 131072 | Indicate notice should not group/collapse with others of the same type (when supported by admin theme) @3.0.146 | |
| Notice::prepend const | 1 | Flag indicates notice should prepend (rather than append) to any existing notices @3.0.135 | |
| Notice::separate const | 131072 | Alias of noGroup flag @3.0.208 | |
| Notice::superuser const | 1048576 | Ignore notice unless current user is a superuser @3.0.149 |
Properties
| Name | Return | Summary | |
|---|---|---|---|
| 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