function DateRangeCustomFormatter::viewElements

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

Overrides DateTimeRangeTrait::viewElements

File

core/modules/datetime_range/src/Plugin/Field/FieldFormatter/DateRangeCustomFormatter.php, line 39

Class

DateRangeCustomFormatter
Plugin implementation of the 'Custom' formatter for 'daterange' fields.

Namespace

Drupal\datetime_range\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
    // @todo Evaluate removing this method in
    // https://www.drupal.org/node/2793143 to determine if the behavior and
    // markup in the base class implementation can be used instead.
    $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] = $this->renderStartEnd($start_date, $separator, $end_date);
            }
            else {
                $elements[$delta] = $this->buildDate($start_date);
            }
        }
    }
    return $elements;
}

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