cool_message-8.x-2.0/cool_message.module
cool_message.module
<?php
/**
* @file
* cool_message.module
* Provides cool notification messages.
*/
use Drupal\Core\Render\Markup;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function cool_message_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'cool_message.admin_config':
return '<p>' . t('Appearance settings for all system messages.') . '</p>';
}
}
/**
* Implements hook_theme().
*/
function cool_message_theme($existing, $type, $theme, $path) {
return array(
'cool_messages' => array(
'variables' => array('title' => NULL, 'type' => NULL, 'style' => NULL, 'messages' => NULL),
'template' => 'cool-messages',
),
);
}
/**
* Process variables for cool-messages.tpl.php.
*
* The $variables array contains the following arguments:
* - title
* - type
* - style
* - messages
*
* @see cool-messages.tpl.php
*/
function template_preprocess_cool_messages(&$variables) {
$status_heading = array(
'status' => t('Status message'),
'error' => t('Error message'),
'warning' => t('Warning message'),
);
$messages = $variables['messages'];
$output = '';
if (!empty($status_heading[$variables['type']])) {
$variables['title'] = $status_heading[$variables['type']];
}
if (count($messages) > 0) {
foreach ($messages as $message) {
if (is_array($message)) {
if ($message instanceof Markup) {
$output .= '<p>' . $message->__toString() . '</p>';
}
else {
$output .= \Drupal::service('renderer')->render($message);
}
}
else {
$output .= '<p>' . $message . '</p>';
}
}
}
// kint($variables);
if (isset($variables['style']) && !empty($variables['style'])) {
$variables['style'] = ' style=background-color:' . $variables['style'] . '';
}
$variables['messages'] = $output;
}
/**
* Implements hook_page_attachments().
*/
function cool_message_page_attachments(array &$attachments) {
// Unconditionally attach an asset to the page.
$attachments['#attached']['library'][] = 'cool_message/cool_message.library';
$config = \Drupal::config('cool_message.cool_message');
$settings = [
'timeout' => $config->get('coolmessage_timeout') * 1000,
];
$attachments['#attached']['drupalSettings']['coll_message'] = $settings;
}
/**
* Implements hook_theme_registry_alter().
*/
function cool_message_theme_registry_alter(&$theme_registry) {
$coolmessage_enable = \Drupal::config('cool_message.cool_message')->get('coolmessage_enable');
if ($coolmessage_enable) {
$theme_registry['status_messages']['function'] = 'cool_message_status_messages';
}
}
/**
* Show themed messages.
*/
function cool_message_status_messages($variables) {
$cm_conf = \Drupal::config('cool_message.cool_message');
$bg_colors = [
'status' => $cm_conf->get('status_color'),
'info' => $cm_conf->get('info_color'),
'warning' => $cm_conf->get('warning_color'),
'error' => $cm_conf->get('error_color')
];
$coolmessage_fixed = $cm_conf->get('coolmessage_position');
$class_fixed = $coolmessage_fixed ? ' class="fixed"' : '';
$output = '<div id="messages"><div id="messages-box" ' . $class_fixed . '>';
foreach ($variables['message_list'] as $type => $messages) {
$template = [
'#theme' => 'cool_messages',
'#type' => $type,
'#style' => isset($bg_colors[$type]) ? $bg_colors[$type] : '',
'#messages' => $messages
];
$output .= \Drupal::service('renderer')->render($template);
}
$output .= "</div></div>";
return $output;
}
