/**
* Remove status message.
*/
function remove_status_message(string $type, callable $callback): bool {
$messenger = \Drupal::messenger();
$messages_by_type = $messenger->messagesByType($type);
$message_deleted = FALSE;
foreach ($messages_by_type as $message_key => $message) {
if ($callback($message)) {
unset($messages_by_type[$message_key]);
$message_deleted = TRUE;
}
}
if ($message_deleted) {
$messenger->deleteByType($type);
foreach ($messages_by_type as $message) {
$messenger->addMessage($message, $type);
}
}
return $message_deleted;
}
Использование:
remove_status_message('error', fn($message) => preg_match('/^Обнаружен. \d+ ошиб/u', (string)$message));
Написанное актуально для
Drupal 8+
Добавить комментарий