messages-2.3.1/messages.module
messages.module
<?php
/**
* @file
* Enhanced Status Messages module.
*
* Provides beautiful, modern status message cards with enhanced styling
* and animations for success, warning, error, and info messages.
*/
use Drupal\Core\Render\Element\StatusMessages;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function messages_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.messages':
$output = '';
$output .= '<h3>' . t('About Enhanced Status Messages') . '</h3>';
$output .= '<p>' . t('The Enhanced Status Messages module provides beautiful, modern status message cards with enhanced styling and animations. It replaces the default Drupal status messages with card-based designs featuring:') . '</p>';
$output .= '<ul>';
$output .= '<li>' . t('Card-based design with shadows and rounded corners') . '</li>';
$output .= '<li>' . t('Decorative wave backgrounds') . '</li>';
$output .= '<li>' . t('Type-specific icons and color schemes') . '</li>';
$output .= '<li>' . t('Smooth animations and transitions') . '</li>';
$output .= '<li>' . t('Configurable auto-hide timers') . '</li>';
$output .= '<li>' . t('Multiple positioning options') . '</li>';
$output .= '<li>' . t('Full accessibility support') . '</li>';
$output .= '<li>' . t('Mobile responsive design') . '</li>';
$output .= '</ul>';
$output .= '<p>' . t('Visit the <a href=":config">configuration page</a> to customize the appearance and behavior of your status messages.', [':config' => \Drupal::url('messages.settings')]) . '</p>';
return $output;
case 'messages.settings':
return '<p>' . t('Configure the appearance and behavior of enhanced status messages. These settings will apply to all status messages displayed on your site.') . '</p>';
}
}
/**
* Implements hook_preprocess_html().
*/
function messages_preprocess_html(&$variables) {
$messenger = \Drupal::messenger();
if ($messenger->all()) {
$variables['#attached']['library'][] = 'messages/enhanced-status-messages';
$config = \Drupal::config('messages.settings');
$variables['#attached']['drupalSettings']['enhancedStatusMessages'] = [
'autoHideTime' => $config->get('auto_hide_time') ?? 5000,
'position' => $config->get('position') ?? 'top-right',
'enableAnimations' => $config->get('enable_animations') ?? TRUE,
'showWaveBackground' => $config->get('show_wave_background') ?? TRUE,
];
}
}
/**
* Implements hook_theme_registry_alter().
*/
function messages_theme_registry_alter(&$theme_registry) {
// Override the default status messages template.
if (isset($theme_registry['status_messages'])) {
$module_path = \Drupal::service('extension.list.module')->getPath('messages');
$theme_registry['status_messages']['path'] = $module_path . '/templates/misc';
}
}
/**
* Implements hook_preprocess_HOOK() for status_messages.
*/
function messages_preprocess_status_messages(&$variables) {
// Pass configuration to template.
$config = \Drupal::config('messages.settings');
$variables['config'] = [
'auto_hide_time' => $config->get('auto_hide_time') ?? 5000,
'position' => $config->get('position') ?? 'top-right',
'enable_animations' => $config->get('enable_animations') ?? TRUE,
'show_wave_background' => $config->get('show_wave_background') ?? TRUE,
'enable_success' => $config->get('enable_success') ?? TRUE,
'enable_warning' => $config->get('enable_warning') ?? TRUE,
'enable_error' => $config->get('enable_error') ?? TRUE,
'enable_info' => $config->get('enable_info') ?? TRUE,
];
}
/**
* Implements hook_preprocess_HOOK() for system_messages_block.
*/
function messages_preprocess_block__system_messages_block(&$variables) {
// Ensure messages are properly rendered with our enhancements.
$variables['content'] = StatusMessages::renderMessages();
$variables['#cache']['max-age'] = 0;
}
/**
* Implements hook_theme().
*/
function messages_theme($existing, $type, $theme, $path) {
return [
'message' => [
'variables' => [
'type' => NULL,
'message' => NULL,
'subText' => NULL,
'showWave' => TRUE,
],
],
];
}
/**
* Implements template_preprocess_HOOK() for message.
*/
function template_preprocess_message(&$variables) {
// Prepare variables for the enhanced status message theme.
$type = $variables['type'];
$variables['type_class'] = 'messages--' . \Drupal\Component\Utility\Html::getClass($type);
// Set default message titles if not provided.
if (empty($variables['message'])) {
switch ($type) {
case 'status':
$variables['message'] = t('Success');
break;
case 'warning':
$variables['message'] = t('Warning');
break;
case 'error':
$variables['message'] = t('Error');
break;
case 'info':
$variables['message'] = t('Information');
break;
}
}
}
/**
* Helper function to add enhanced status message programmatically.
*
* @param string $type
* The message type (status, warning, error, info).
* @param string $message
* The message text.
* @param string $sub_text
* Optional sub-text for additional context.
*/
function messages_add($type, $message, $sub_text = '') {
$messenger = \Drupal::messenger();
// Format message with sub-text if provided.
$formatted_message = $message;
if (!empty($sub_text)) {
$formatted_message = $message . '<br><small>' . $sub_text . '</small>';
}
switch ($type) {
case 'success':
case 'status':
$messenger->addStatus($formatted_message);
break;
case 'warning':
$messenger->addWarning($formatted_message);
break;
case 'error':
$messenger->addError($formatted_message);
break;
case 'info':
default:
$messenger->addMessage($formatted_message);
break;
}
}
