public function DateRangeCustomFormatter::viewElements in Drupal 8
Same name and namespace in other branches
- 9 core/modules/datetime_range/src/Plugin/Field/FieldFormatter/DateRangeCustomFormatter.php \Drupal\datetime_range\Plugin\Field\FieldFormatter\DateRangeCustomFormatter::viewElements()
- 10 core/modules/datetime_range/src/Plugin/Field/FieldFormatter/DateRangeCustomFormatter.php \Drupal\datetime_range\Plugin\Field\FieldFormatter\DateRangeCustomFormatter::viewElements()
Builds a renderable array for a field value.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.
string $langcode: The language that should be used to render the field.
Return value
array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.
Overrides DateTimeRangeTrait::viewElements
File
- core/
modules/ datetime_range/ src/ Plugin/ Field/ FieldFormatter/ DateRangeCustomFormatter.php, line 40
Class
- DateRangeCustomFormatter
- Plugin implementation of the 'Custom' formatter for 'daterange' fields.
Namespace
Drupal\datetime_range\Plugin\Field\FieldFormatterCode
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] = [
'start_date' => $this
->buildDate($start_date),
'separator' => [
'#plain_text' => ' ' . $separator . ' ',
],
'end_date' => $this
->buildDate($end_date),
];
}
else {
$elements[$delta] = $this
->buildDate($start_date);
}
}
}
return $elements;
}