You are here

function extra_body_classes_preprocess_html in Extra body classes 8

Same name and namespace in other branches
  1. 7 extra_body_classes.module \extra_body_classes_preprocess_html()

Implements hook_preprocess_html().

File

./extra_body_classes.module, line 56
Implements extra_body_classes module.

Code

function extra_body_classes_preprocess_html(&$variables) {
  $route = \Drupal::routeMatch()
    ->getRouteObject();
  $is_admin = \Drupal::service('router.admin_context')
    ->isAdminRoute($route);
  if (!$is_admin) {
    $variables['#attached']['library'][] = 'extra_body_classes/extra_body_classes_js';
    $config = \Drupal::config('extra_body_classes.settings');

    // Platform on which browser is running.
    if ($config
      ->get('extra_body_classes_browser_platform')) {
      $platform = $config
        ->get('extra_body_classes_browser_platform');
      $variables['#attached']['drupalSettings']['extra_body_classes']['platform'] = $platform;
    }

    // Name and version of browser.
    if ($config
      ->get('extra_body_classes_browser_name_version')) {
      $name_version = $config
        ->get('extra_body_classes_browser_name_version');
      $variables['#attached']['drupalSettings']['extra_body_classes']['name_version'] = $name_version;
    }

    // Check whether device is desktop or mobile.
    if ($config
      ->get('extra_body_classes_browser_device')) {
      $device = $config
        ->get('extra_body_classes_browser_device');
      $variables['#attached']['drupalSettings']['extra_body_classes']['device'] = $device;
    }

    // Current date timestamp.
    if ($config
      ->get('extra_body_classes_date')) {
      $variables['attributes']['class'][] = date('Ymd');
    }

    // Current year.
    if ($config
      ->get('extra_body_classes_year')) {
      $variables['attributes']['class'][] = date('Y');
    }

    // Current month.
    if ($config
      ->get('extra_body_classes_month')) {
      $variables['attributes']['class'][] = lcfirst(date('F'));
    }

    // Current day.
    if ($config
      ->get('extra_body_classes_day')) {
      $variables['attributes']['class'][] = lcfirst(date('l'));
    }

    // User roles.
    if ($config
      ->get('extra_body_classes_roles')) {
      $currentuserroles = \Drupal::currentUser()
        ->getRoles();
      foreach ($currentuserroles as $role) {
        $variables['attributes']['class'][] = $role;
      }
    }

    // Take todays date.
    $timestamp_current_date = date("Y-m-d");
    $timestamp_current_date = strtotime($timestamp_current_date);

    // Single day event.
    $extra_body_classes_browser_single_day_event_begins = $config
      ->get('extra_body_classes_browser_single_day_event_begins');
    $timestamp_single_day_event = strtotime($extra_body_classes_browser_single_day_event_begins);

    // Check single day event match with current date.
    if ($timestamp_single_day_event == $timestamp_current_date) {
      $extra_body_classes_browser_single_day_event = $config
        ->get('extra_body_classes_browser_single_day_event');
      $extra_body_classes_browser_single_day_event = explode(',', $extra_body_classes_browser_single_day_event);
      foreach ($extra_body_classes_browser_single_day_event as $single_day_event_classes) {
        $variables['attributes']['class'][] = $single_day_event_classes;
      }
    }

    // Multiple day event.
    $extra_body_classes_event_start_date = $config
      ->get('extra_body_classes_event_start_date');
    $extra_body_classes_event_end_date = $config
      ->get('extra_body_classes_event_end_date');
    $timestamp_start_date = strtotime($extra_body_classes_event_start_date);
    $timestamp_end_date = strtotime($extra_body_classes_event_end_date);
    if ($timestamp_current_date >= $timestamp_start_date && $timestamp_current_date <= $timestamp_end_date) {
      $extra_body_classes_event = $config
        ->get('extra_body_classes_event');
      $extra_body_classes_event = explode(',', $extra_body_classes_event);
      foreach ($extra_body_classes_event as $event_classes) {
        $variables['attributes']['class'][] = $event_classes;
      }
    }

    // Get current path.
    $extra_body_classes_get_current_path = \Drupal::service('path.current')
      ->getPath();
    $extra_body_classes_get_current_path_alias = \Drupal::service('path_alias.manager')
      ->getAliasByPath($extra_body_classes_get_current_path);

    // Removing '/' form current path.
    $extra_body_classes_get_current_path_alias = substr($extra_body_classes_get_current_path_alias, 1);

    // Get all custom path.
    $extra_body_classes_get_all_path = $config
      ->get('extra_body_classes_custom_classes_path');
    $extra_body_classes_pages = explode(PHP_EOL, $extra_body_classes_get_all_path);

    // Custom Classes.
    $extra_body_classes_custom_classes = $config
      ->get('extra_body_classes_custom_classes');
    $extra_body_classes_custom_classes = explode(',', $extra_body_classes_custom_classes);

    // Check current path and implemet custom classes.
    if (strlen($extra_body_classes_get_all_path) > 0) {
      foreach ($extra_body_classes_pages as $extra_body_classes_pages_value) {
        if (\Drupal::service('path.matcher')
          ->matchPath($extra_body_classes_get_current_path_alias, $extra_body_classes_pages_value)) {
          foreach ($extra_body_classes_custom_classes as $custom_classes) {
            $variables['attributes']['class'][] = $custom_classes;
          }
        }
        elseif (\Drupal::service('path.matcher')
          ->matchPath($extra_body_classes_get_current_path, $extra_body_classes_pages_value)) {
          foreach ($extra_body_classes_custom_classes as $custom_classes) {
            $variables['attributes']['class'][] = $custom_classes;
          }
        }
      }
    }
  }
}