function DatePreprocess::preprocessTime

Prepares variables for time templates.

Default template: time.html.twig.

Parameters

array $variables: An associative array possibly containing:

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

File

core/lib/Drupal/Core/Datetime/DatePreprocess.php, line 33

Class

DatePreprocess
Preprocess for common/core theme templates.

Namespace

Drupal\Core\Datetime

Code

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']);
    }
    elseif (isset($variables['attributes']['datetime'])) {
      $variables['text'] = $variables['attributes']['datetime'];
    }
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.