trait DateTimeRangeTrait

Same name in other branches
  1. 9 core/modules/datetime_range/src/DateTimeRangeTrait.php \Drupal\datetime_range\DateTimeRangeTrait
  2. 10 core/modules/datetime_range/src/DateTimeRangeTrait.php \Drupal\datetime_range\DateTimeRangeTrait
  3. 11.x core/modules/datetime_range/src/DateTimeRangeTrait.php \Drupal\datetime_range\DateTimeRangeTrait

Provides friendly methods for datetime range.

Hierarchy

3 files declare their use of DateTimeRangeTrait
DateRangeCustomFormatter.php in core/modules/datetime_range/src/Plugin/Field/FieldFormatter/DateRangeCustomFormatter.php
DateRangeDefaultFormatter.php in core/modules/datetime_range/src/Plugin/Field/FieldFormatter/DateRangeDefaultFormatter.php
DateRangePlainFormatter.php in core/modules/datetime_range/src/Plugin/Field/FieldFormatter/DateRangePlainFormatter.php

File

core/modules/datetime_range/src/DateTimeRangeTrait.php, line 10

Namespace

Drupal\datetime_range
View source
trait DateTimeRangeTrait {
    
    /**
     * {@inheritdoc}
     */
    public function viewElements(FieldItemListInterface $items, $langcode) {
        $elements = [];
        $separator = $this->getSetting('separator');
        foreach ($items as $delta => $item) {
            if (!empty($item->start_date) && !empty($item->end_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;
                if ($start_date->getTimestamp() !== $end_date->getTimestamp()) {
                    $elements[$delta] = [
                        'start_date' => $this->buildDateWithIsoAttribute($start_date),
                        'separator' => [
                            '#plain_text' => ' ' . $separator . ' ',
                        ],
                        'end_date' => $this->buildDateWithIsoAttribute($end_date),
                    ];
                }
                else {
                    $elements[$delta] = $this->buildDateWithIsoAttribute($start_date);
                    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;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
DateTimeRangeTrait::viewElements public function 2

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