template_preprocess_time

template_preprocess_time(&$variables)

Prepares variables for time templates.

Default template: time.html.twig.

Parameters

array $variables: An associative array possibly containing:

  • attributes['timestamp']:
  • timestamp:
  • text:

File

core/includes/theme.inc, line 509
The theme system, which controls the output of Drupal.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function template_preprocess_time(&$variables) {
  // Format the 'datetime' attribute based on the timestamp.
  if (!isset($variables['attributes']['datetime']) && isset($variables['timestamp'])) {
    $variables['attributes']['datetime'] = format_date($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'] = format_date($variables['timestamp']);
    }
    // Otherwise, use the literal datetime attribute.
    elseif (isset($variables['attributes']['datetime'])) {
      $variables['text'] = $variables['attributes']['datetime'];
    }
  }
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.