summaryrefslogtreecommitdiffstatshomepage
path: root/core/lib/Drupal/Core/Datetime/DatePreprocess.php
blob: 6206288b0b3bef453f8c535acbef80b485517efc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php

namespace Drupal\Core\Datetime;

use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Template\Attribute;

/**
 * Preprocess for common/core theme templates.
 *
 * @internal
 */
class DatePreprocess {

  use StringTranslationTrait;

  public function __construct(
    protected DateFormatterInterface $dateFormatter,
  ) {
  }

  /**
   * Prepares variables for time templates.
   *
   * Default template: time.html.twig.
   *
   * @param array $variables
   *   An associative array possibly containing:
   *   - "attributes['timestamp']:".
   *   - "timestamp:".
   *   - "text:".
   */
  public function preprocessTime(array &$variables): void {
    // Format the 'datetime' attribute based on the timestamp.
    // @see https://www.w3.org/TR/html5-author/the-time-element.html#attr-time-datetime
    if (!isset($variables['attributes']['datetime']) && isset($variables['timestamp'])) {
      $variables['attributes']['datetime'] = $this->dateFormatter->format($variables['timestamp'], 'html_datetime', '', 'UTC');
    }

    // If no text was provided, try to auto-generate it.
    if (!isset($variables['text'])) {
      // Format and use a human-readable version of the timestamp, if any.
      if (isset($variables['timestamp'])) {
        $variables['text'] = $this->dateFormatter->format($variables['timestamp']);
      }
      // Otherwise, use the literal datetime attribute.
      elseif (isset($variables['attributes']['datetime'])) {
        $variables['text'] = $variables['attributes']['datetime'];
      }
    }
  }

  /**
   * Prepares variables for datetime form element templates.
   *
   * The datetime form element serves as a wrapper around the date element type,
   * which creates a date and a time component for a date.
   *
   * Default template: datetime-form.html.twig.
   *
   * @param array $variables
   *   An associative array containing:
   *   - element: An associative array containing the properties of the element.
   *     Properties used: #title, #value, #options, #description, #required,
   *     #attributes.
   *
   * @see form_process_datetime()
   */
  public function preprocessDatetimeForm(array &$variables): void {
    $element = $variables['element'];

    $variables['attributes'] = [];

    if (isset($element['#id'])) {
      $variables['attributes']['id'] = $element['#id'];
    }
    if (!empty($element['#attributes']['class'])) {
      $variables['attributes']['class'] = (array) $element['#attributes']['class'];
    }

    $variables['content'] = $element;
  }

  /**
   * Prepares variables for datetime form wrapper templates.
   *
   * Default template: datetime-wrapper.html.twig.
   *
   * @param array $variables
   *   An associative array containing:
   *   - element: An associative array containing the properties of the element.
   *     Properties used: #title, #children, #required, #attributes.
   */
  public function preprocessDatetimeWrapper(array &$variables): void {
    $element = $variables['element'];

    if (!empty($element['#title'])) {
      $variables['title'] = $element['#title'];
      // If the element title is a string, wrap it a render array so that markup
      // will not be escaped (but XSS-filtered).
      if (is_string($variables['title']) && $variables['title'] !== '') {
        $variables['title'] = ['#markup' => $variables['title']];
      }
    }

    // Suppress error messages.
    $variables['errors'] = NULL;

    $variables['description'] = NULL;
    if (!empty($element['#description'])) {
      $description_attributes = [];
      if (!empty($element['#id'])) {
        $description_attributes['id'] = $element['#id'] . '--description';
      }
      $description_attributes['data-drupal-field-elements'] = 'description';
      $variables['description'] = $element['#description'];
      $variables['description_attributes'] = new Attribute($description_attributes);
    }

    $variables['required'] = FALSE;
    // For required datetime fields 'form-required' & 'js-form-required' classes
    // are appended to the label attributes.
    if (!empty($element['#required'])) {
      $variables['required'] = TRUE;
    }
    $variables['content'] = $element['#children'];
  }

}