bootstrap_site_alert.module in Bootstrap Site Alert 8
Same filename and directory in other branches
The bootstrap_site_alert module file.
File
bootstrap_site_alert.moduleView source
<?php
/**
* @file
* The bootstrap_site_alert module file.
*/
use Drupal\Core\Url;
/**
* Implements hook_page_top().
*/
function bootstrap_site_alert_page_top(array &$page_top) {
// Grab our number of alerts.
$count = \Drupal::state()
->get('bootstrap_site_alert_count', 0);
if (!$count) {
return;
}
for ($i = 0; $i < $count; $i++) {
// Should we show on admin pages?
$show_admin = \Drupal::state()
->get('bootstrap_site_alert_no_admin' . $i, 0) ? !\Drupal::service('router.admin_context')
->isAdminRoute() : TRUE;
// Checks if we should exclude on the current page.
$page_match = TRUE;
$page_match_enabled = \Drupal::state()
->get('bootstrap_site_alert_exclude' . $i, 0);
$paths = mb_strtolower(\Drupal::state()
->get('bootstrap_site_alert_only_paths' . $i, ''));
// Adjust for the front page.
$paths = str_replace('<front>', ltrim(\Drupal::config('system.site')
->get('page.front'), '/'), $paths);
if ($page_match_enabled && !empty($paths)) {
$path = ltrim(\Drupal::service('path.current')
->getPath(), '/');
$page_match = \Drupal::service('path.matcher')
->matchPath($path, $paths);
}
// Set the message if need be..
if (\Drupal::state()
->get('bootstrap_site_alert_active' . $i, 0) && \Drupal::currentUser()
->hasPermission('view bootstrap site alerts') && $show_admin && $page_match) {
// Get variables.
$level = \Drupal::state()
->get('bootstrap_site_alert_severity' . $i, NULL);
$message = \Drupal::state()
->get('bootstrap_site_alert_message' . $i, NULL);
$alert = '<div class="alert bs-site-alert ' . $level . '" role="alert"';
// If dismissible, add 'close' button. Also add 'display:none' to the
// alert element to prevent it from momentarily flickering onscreen
// before we have a chance to hide it.
if (\Drupal::state()
->get('bootstrap_site_alert_dismiss' . $i, 0)) {
$alert .= ' style="display:none;">';
$alert .= '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>';
}
else {
$alert .= '>';
}
$alert .= isset($message['value']) ? t($message['value']) : NULL;
$alert .= '</div>';
$page_top['bootstrap_site_alert' . $i] = [
'#type' => 'inline_template',
'#template' => $alert,
'#weight' => 1000,
];
}
// If dismissable, attach JavaScript file and configure drupalSettings.
if (\Drupal::state()
->get('bootstrap_site_alert_dismiss' . $i)) {
// A random key is generated whenever an alert has changed. Pass this key
// to drupalSettings so that it is accessible via JavaScript.
$key = \Drupal::state()
->get('bootstrap_site_alert_key');
$page_top['bootstrap_site_alert']['#attached'] = [
'library' => [
'bootstrap_site_alert/dismissed-cookie',
],
'drupalSettings' => [
'bootstrap_site_alert' => [
'dismissedCookie' => [
'key' => $key,
],
],
],
];
}
}
}
Functions
Name | Description |
---|---|
bootstrap_site_alert_page_top | Implements hook_page_top(). |