comparison core/modules/datetime_range/src/DateTimeRangeTrait.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2
3 namespace Drupal\datetime_range;
4
5 use Drupal\Core\Field\FieldItemListInterface;
6
7 /**
8 * Provides friendly methods for datetime range.
9 */
10 trait DateTimeRangeTrait {
11
12 /**
13 * {@inheritdoc}
14 */
15 public function viewElements(FieldItemListInterface $items, $langcode) {
16 $elements = [];
17 $separator = $this->getSetting('separator');
18
19 foreach ($items as $delta => $item) {
20 if (!empty($item->start_date) && !empty($item->end_date)) {
21 /** @var \Drupal\Core\Datetime\DrupalDateTime $start_date */
22 $start_date = $item->start_date;
23 /** @var \Drupal\Core\Datetime\DrupalDateTime $end_date */
24 $end_date = $item->end_date;
25
26 if ($start_date->getTimestamp() !== $end_date->getTimestamp()) {
27 $elements[$delta] = [
28 'start_date' => $this->buildDateWithIsoAttribute($start_date),
29 'separator' => ['#plain_text' => ' ' . $separator . ' '],
30 'end_date' => $this->buildDateWithIsoAttribute($end_date),
31 ];
32 }
33 else {
34 $elements[$delta] = $this->buildDateWithIsoAttribute($start_date);
35
36 if (!empty($item->_attributes)) {
37 $elements[$delta]['#attributes'] += $item->_attributes;
38 // Unset field item attributes since they have been included in the
39 // formatter output and should not be rendered in the field template.
40 unset($item->_attributes);
41 }
42 }
43 }
44 }
45
46 return $elements;
47 }
48
49 }