DateTimeRangeTrait.php in Drupal 10
File
core/modules/datetime_range/src/DateTimeRangeTrait.php
View source
<?php
namespace Drupal\datetime_range;
use Drupal\Core\Field\FieldItemListInterface;
trait DateTimeRangeTrait {
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)) {
$start_date = $item->start_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($item->_attributes);
}
}
}
}
return $elements;
}
}