extra_body_classes.module in Extra body classes 8
Same filename and directory in other branches
Implements extra_body_classes module.
File
extra_body_classes.moduleView source
<?php
/**
* @file
* Implements extra_body_classes module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function extra_body_classes_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.extra_body_classes':
$output = '';
$output .= '<h3>' . t('CONTENTS OF THIS FILE') . '</h3>';
$output .= '<ol>';
$output .= '<li>' . t('INTRODUCTION') . '</li>';
$output .= '<li>' . t('INSTALLATION') . '</li>';
$output .= '<li>' . t('HOW TO USE IT') . '</li>';
$output .= '</ol>';
$output .= '<h3>' . t('INTRODUCTION') . '</h3>';
$output .= '<p>' . t('Extra body classes is a simple module which will use to provide this classes to body tag.') . '</p>';
$output .= '<ol>';
$output .= '<li>' . t('current date time stamp') . '</li>';
$output .= '<li>' . t('current year') . '</li>';
$output .= '<li>' . t('current month') . '</li>';
$output .= '<li>' . t('current day') . '</li>';
$output .= '<li>' . t('current user roles') . '</li>';
$output .= '<li>' . t('custom classe') . '</li>';
$output .= '</ol>';
$output .= '<h3>' . t('INSTALLATION') . '</h3>';
$output .= '<ol>';
$output .= '<li>' . t('Download and place unzipped folder in /modules/contrib directory.') . '</li>';
$output .= '<li>' . t('Go to admin/modules path and install this module.') . '</li>';
$output .= '<li>' . t('Now Extra body classes module is ready to use.') . '</li>';
$output .= '</ol>';
$output .= '<h3>' . t('HOW TO USE IT') . '</h3>';
$output .= '<ol>';
$output .= '<li>' . t('Once this module is successfully installed, go to admin/config/content/extra-body-classes path.') . '</li>';
$output .= '<li>' . t('If user needs to add current date time stamp as a body class then select Date check box.') . '</li>';
$output .= '<li>' . t('If user needs to add current year as a body class then select Year check box.') . '</li>';
$output .= '<li>' . t('If user needs to add current month as a body class then select Month check box.') . '</li>';
$output .= '<li>' . t('If user needs to add current day as a body class then select Day check box.') . '</li>';
$output .= '<li>' . t('If user needs to add current user roles as a body class then select Current user roles check box.') . '</li>';
$output .= '<li>' . t('If user needs to add custom class then provide it into Add custom classes filed.') . '</li>';
$output .= '<li>' . t('Save it') . '</li>';
$output .= '</ol>';
return $output;
}
}
/**
* Implements hook_preprocess_html().
*/
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;
}
}
}
}
}
}
Functions
Name | Description |
---|---|
extra_body_classes_help | Implements hook_help(). |
extra_body_classes_preprocess_html | Implements hook_preprocess_html(). |