You are here

function hijri_preprocess_node in Hijri 3.0.x

Same name and namespace in other branches
  1. 8.2 hijri.module \hijri_preprocess_node()
  2. 8 hijri.module \hijri_preprocess_node()
  3. 7 hijri.module \hijri_preprocess_node()
  4. 1.0.x hijri.module \hijri_preprocess_node()

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.

Parameters

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.

File

./hijri.module, line 103
This module provides Hijri Date integration with Drupal core date field and with other Drupal contributions such as Views and Date.

Code

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;
      }
    }
  }
}