You are here

hijri.module in Hijri 3.0.x

Same filename and directory in other branches
  1. 8.2 hijri.module
  2. 8 hijri.module
  3. 7 hijri.module
  4. 1.0.x hijri.module

This module provides Hijri Date integration with Drupal core date field and with other Drupal contributions such as Views and Date.

File

hijri.module
View source
<?php

/**
 * @file
 * This module provides Hijri Date integration with Drupal core date field and with other Drupal contributions such as Views and Date.
 */
use Drupal\Core\Render\Element\Date;
use Drupal\Core\Datetime\Element\Datetime;
use Drupal\hijri\Element\HijriDateList;
use Drupal\Core\Datetime\Element\Datelist;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function hijri_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.saudi_identity':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('This module provides Hijri Date integration with Drupal core date field and with other Drupal contributions such as Views and Date.') . '</p>';
      return $output;
  }
}

/**
 * Implements hook_field_formatter_info_alter().
 */
function hijri_field_formatter_info_alter(&$info) {
  $info['string']['field_types'][] = 'hijri';
}

/**
 * Implements hook_element_info_alter().
 *
 * Swap drupal elements with calendar_systems elements.
 */
function hijri_element_info_alter(array &$info) {
  $rep = function ($find_for) {
    switch ($find_for) {
      case Datelist::class:
        $rep = HijriDateList::class;
        break;
      case Datetime::class:
        $rep = NULL;
        break;
      case Date::class:
        $rep = NULL;
        break;
      default:
        $rep = NULL;
    }
    return $rep;
  };
  foreach ([
    'date',
    'datelist',
    'datetime',
  ] as $el_) {
    if (!isset($info[$el_])) {
      continue;
    }
    $el =& $info[$el_];
    if ($el_ === 'date' || $el_ === 'datetime') {
      $el['#attached']['library'][] = 'calendar_systems/picker';
    }
    foreach ([
      '#process',
      '#pre_render',
      '#element_validate',
      '#value_callback',
    ] as $attr_) {
      if (!isset($el[$attr_])) {
        continue;
      }
      foreach ($el[$attr_] as $ai => &$a) {
        if (is_array($a)) {
          $a[0] = $rep($a[0]) ?: $a[0];
        }
        else {
          $el[$attr_][$ai] = $rep($a) ?: $a;
        }
      }
    }
  }
}

/**
 * Prepares variables for node templates.
 *
 * Default template: node.html.twig.
 *
 * Most themes use their own copy of node.html.twig. The default is located
 * inside "/core/modules/node/templates/node.html.twig". Look in there for the
 * full list of variables.
 *
 * @param array $variables
 *   An associative array containing:
 *   - elements: An array of elements to display in view mode.
 *   - node: The node object.
 *   - view_mode: View mode; e.g., 'full', 'teaser', etc.
 */
function hijri_preprocess_node(array &$variables) {
  $node = $variables['node'];
  $node_types = \Drupal::config('hijri.config')
    ->get('hijri_types');
  $hijri_display = \Drupal::config('hijri.config')
    ->get('hijri_display');

  /** @var \Drupal\hijri\HijriFormatter $hijri_formatter */
  $hijri_formatter = \Drupal::service('hijri.formatter');
  if ($hijri_display != 'none') {
    if (isset($node_types[$node
      ->getType()]) && (string) $node_types[$node
      ->getType()] == $node
      ->getType()) {
      switch ($hijri_display) {
        case 'full':
          $format = t('@hijri on @gregorian', [
            '@hijri' => $hijri_formatter
              ->format($node
              ->getCreatedTime(), 'custom', 'l j F Y'),
            '@gregorian' => \Drupal::service('date.formatter')
              ->format($node
              ->getCreatedTime(), 'custom', 'F j, Y'),
          ]);
          break;
        case 'long':
        case 'medium':
        case 'short':
          $format = $hijri_formatter
            ->format($node
            ->getCreatedTime(), $hijri_display);
          break;
        default:
          $format = $node
            ->getCreatedTime();
          break;
      }
      $node_type = $node->type->entity;

      // Display post information only on certain node types.
      if ($node_type
        ->displaySubmitted()) {
        $variables['display_submitted'] = TRUE;
        $variables['date'] = $format;
      }
    }
  }
}

/**
 * Prepares variables for comment templates.
 *
 * Default template: comment.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - elements: An associative array containing the comment and entity objects.
 *     Array keys: #comment, #commented_entity.
 */
function hijri_preprocess_comment(array &$variables) {
  $comment = $variables['elements']['#comment'];
  $node = $comment
    ->getCommentedEntity();
  $variables['comment'] = $comment;
  $node_types = \Drupal::config('hijri.config')
    ->get('hijri_types');
  $hijri_display = \Drupal::config('hijri.config')
    ->get('hijri_comment_display');

  /** @var \Drupal\hijri\HijriFormatter $hijri_formatter */
  $hijri_formatter = \Drupal::service('hijri.formatter');
  if ($hijri_display != 'none') {
    if (isset($node_types[$node
      ->getType()]) && (string) $node_types[$node
      ->getType()] == $node
      ->getType()) {
      switch ($hijri_display) {
        case 'full':
          $format = t('@hijri on @gregorian', [
            '@hijri' => $hijri_formatter
              ->format($comment
              ->getCreatedTime(), 'custom', 'l j F Y'),
            '@gregorian' => \Drupal::service('date.formatter')
              ->format($comment
              ->getCreatedTime(), 'custom', 'F j, Y'),
          ]);
          break;
        case 'long':
        case 'medium':
        case 'short':
          $format = $hijri_formatter
            ->format($comment
            ->getCreatedTime(), $hijri_display);
          break;
        default:
          $format = $variables['created'];
          break;
      }
      $variables['submitted'] = t('Submitted by @username on @datetime', [
        '@username' => $variables['author'],
        '@datetime' => $format,
      ]);
      $variables['created'] = $format;
    }
  }
}

Functions

Namesort descending Description
hijri_element_info_alter Implements hook_element_info_alter().
hijri_field_formatter_info_alter Implements hook_field_formatter_info_alter().
hijri_help Implements hook_help().
hijri_preprocess_comment Prepares variables for comment templates.
hijri_preprocess_node Prepares variables for node templates.