You are here

function bootstrap_site_alert_page_alter in Bootstrap Site Alert 7

Implements hook_page_alter().

File

./bootstrap_site_alert.module, line 44
The bootstrap_site_alert module file.

Code

function bootstrap_site_alert_page_alter(&$page) {

  // Should we show on admin pages?
  $show_admin = variable_get('bootstrap_site_alert_no_admin', 0) ? !path_is_admin(current_path()) : TRUE;

  // If active this is set.
  if (variable_get('bootstrap_site_alert_active', FALSE) && user_access('view bootstrap site alerts') && $show_admin) {

    // Get variables.
    $level = variable_get('bootstrap_site_alert_severity', NULL);
    $message = variable_get('bootstrap_site_alert_message', array(
      'value' => '',
      'format' => NULL,
    ));

    // Start building the element.
    $alert = '<div class="alert bs-site-alert ' . $level . '" role="alert"';

    // Special handling for dismissable alerts.
    if (variable_get('bootstrap_site_alert_dismiss', FALSE)) {

      // Load some JS for toggling visible state based on cookie value. If
      // dismiss is triggered, the alert will be hidden for the session
      // duration.
      $page['content']['#attached']['library'][] = array(
        'system',
        'jquery.cookie',
      );
      $page['content']['#attached']['js'][] = array(
        'type' => 'file',
        'data' => drupal_get_path('module', 'bootstrap_site_alert') . '/js/bootstrap-site-alert.js',
      );

      // A random key is generated whenever an alert has changed.
      // Make this key accessible via JavaScript.
      drupal_add_js(array(
        'bootstrap_site_alert' => array(
          'key' => variable_get('bootstrap_site_alert_key', '123'),
        ),
      ), array(
        'type' => 'setting',
      ));

      // Add 'display:none' to the alert element. This prevents it from
      // momentarily flickering onscreen before we have a chance to hide it.
      $alert .= ' style="display:none;">';

      // Add close button.
      $alert .= '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
    }
    else {
      $alert .= '>';
    }
    $alert .= $message['value'];
    $alert .= '</div>';
    $page['page_top']['bootstrap_site_alert'] = array(
      '#markup' => $alert,
      '#weight' => 1000,
    );
  }
}