Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. theme.inc

function template_preprocess_time

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 337

Code

function template_preprocess_time(&$variables) : void {
    
    /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
    $date_formatter = \Drupal::service('date.formatter');
    // 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'] = $date_formatter->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'] = $date_formatter->format($variables['timestamp']);
        }
        elseif (isset($variables['attributes']['datetime'])) {
            $variables['text'] = $variables['attributes']['datetime'];
        }
    }
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal