Skip to main content

Alerts

Alerts are a very basic way to display a message on a resource page. By default, it is only shown on detail.

Creating an alert

Alert has a $model you can access (only on detail & edit). Alerts are very straightforward:

class UserBlocked extends Alert
{
public function title(): string
{
return __('User blocked');
}

public function description(): string
{
if (!empty($this->model->block_reason)) {
$message = __('Block reason: :reason', ['reason' => $this->model->block_reason]);
} else {
$message = __('This user is blocked from logging in');
}

return $message;
}

public function iconColor(): string
{
return 'negative';
}

public function icon(): string
{
return 'block';
}
}

You can make an alert not dismissable:

public function canBeClosed(): bool
{
return false;
}

Registering an alert

You can register an alert on the resource:

public function alerts(): AlertCollection
{
return new AlertCollection(
new UserBlocked
);
}

Visibility

Alerts, by default, are shown on detail and hidden on other pages.

You can conditionally show an alert on detail:

public function alerts(): AlertCollection
{
return new AlertCollection(
(new UserBlocked)
->hideOnShow(fn(UserModel $user) => !$user->blocked)
);
}

Or for example show only on edit:

public function alerts(): AlertCollection
{
return new AlertCollection(
(new UserBlocked)
->hideOnShow(true)
->hideOnEdit(fn(UserModel $user) => !$user->blocked)
);
}

Creating alert actions

When dealing with alerts, you might find it beneficial to include specific actions tied to a particular alert. For instance, consider an alert associated with a resource:

public function alerts(): AlertCollection
{
return new AlertCollection(
new ResourceAlert
);
}

Within the alert class, you have the freedom to specify a range of actions:

class ResourceAlert extends Alert
{
public function actions(): ActionCollection
{
return new ActionCollection(
(new ResourceAlertAction1),
(new ResourceAlertAction2),
);
}
}
info

Make sure that any actions created here extend Qore\System\Resource\Alert\Action;