function DateTimeFormatterBase::buildDateWithIsoAttribute

Same name in other branches
  1. 9 core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php \Drupal\datetime\Plugin\Field\FieldFormatter\DateTimeFormatterBase::buildDateWithIsoAttribute()
  2. 8.9.x core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php \Drupal\datetime\Plugin\Field\FieldFormatter\DateTimeFormatterBase::buildDateWithIsoAttribute()
  3. 10 core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php \Drupal\datetime\Plugin\Field\FieldFormatter\DateTimeFormatterBase::buildDateWithIsoAttribute()

Creates a render array from a date object with ISO date attribute.

Parameters

\Drupal\Core\Datetime\DrupalDateTime $date: A date object.

Return value

array A render array.

1 call to DateTimeFormatterBase::buildDateWithIsoAttribute()
DateTimeFormatterBase::viewElements in core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php
Builds a renderable array for a field value.

File

core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php, line 226

Class

DateTimeFormatterBase
Base class for 'DateTime Field formatter' plugin implementations.

Namespace

Drupal\datetime\Plugin\Field\FieldFormatter

Code

protected function buildDateWithIsoAttribute(DrupalDateTime $date) {
    // Create the ISO date in Universal Time.
    $iso_date = $date->format("Y-m-d\\TH:i:s") . 'Z';
    $this->setTimeZone($date);
    $build = [
        '#theme' => 'time',
        '#text' => $this->formatDate($date),
        '#attributes' => [
            'datetime' => $iso_date,
        ],
        '#cache' => [
            'contexts' => [
                'timezone',
            ],
        ],
    ];
    return $build;
}

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