You are here

public function DateRangeAllDayTrait::viewElements in Date all day 8

See also

Drupal\date_all_day\DateRangeAllDayTrait

File

src/Plugin/Field/FieldFormatter/DateRangeAllDayTrait.php, line 19

Class

DateRangeAllDayTrait
A viewElements method, that respects an empty end date.

Namespace

Drupal\date_all_day\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  $separator = $this
    ->getSetting('separator');
  foreach ($items as $delta => $item) {
    if (!empty($item->start_date)) {

      /** @var \Drupal\Core\Datetime\DrupalDateTime $start_date */
      $start_date = $item->start_date;

      /** @var \Drupal\Core\Datetime\DrupalDateTime $end_date */
      $end_date = $item->end_date;
      $item_value = $item
        ->getValue();
      $is_all_day = DateRangeAllDayHelper::isAllDay($item);
      if ($end_date !== NULL && $start_date
        ->getTimestamp() !== $end_date
        ->getTimestamp()) {
        $elements[$delta] = [
          'start_date' => $this
            ->buildDateWithIsoAttribute($start_date, $is_all_day),
          'separator' => [
            '#plain_text' => ' ' . $separator . ' ',
          ],
          'end_date' => $this
            ->buildDateWithIsoAttribute($end_date, $is_all_day),
        ];
      }
      else {
        $elements[$delta] = $this
          ->buildDateWithIsoAttribute($start_date, $is_all_day);
        if (!empty($item->_attributes)) {
          $elements[$delta]['#attributes'] += $item->_attributes;

          // Unset field item attributes since they have been included in the
          // formatter output and should not be rendered in the field template.
          unset($item->_attributes);
        }
      }
    }
  }
  return $elements;
}