You are here

protected function DateTimeDayDefaultFormatter::buildDateWithIsoAttribute in Date time day 8

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.

Overrides DateTimeFormatterBase::buildDateWithIsoAttribute

File

src/Plugin/Field/FieldFormatter/DateTimeDayDefaultFormatter.php, line 145

Class

DateTimeDayDefaultFormatter
Plugin implementation of the 'Default' formatter for 'datetimeday' fields.

Namespace

Drupal\date_time_day\Plugin\Field\FieldFormatter

Code

protected function buildDateWithIsoAttribute(DrupalDateTime $date) {
  if ($this
    ->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) {

    // A date without time will pick up the current time, use the default.
    $date
      ->setDefaultDateTime();
  }

  // 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),
    '#html' => FALSE,
    '#attributes' => [
      'datetime' => $iso_date,
    ],
    '#cache' => [
      'contexts' => [
        'timezone',
      ],
    ],
  ];
  return $build;
}