comparison core/modules/datetime/src/DateTimeComputed.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
5 use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem; 5 use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
6 use Drupal\Core\Datetime\DrupalDateTime; 6 use Drupal\Core\Datetime\DrupalDateTime;
7 use Drupal\Core\TypedData\DataDefinitionInterface; 7 use Drupal\Core\TypedData\DataDefinitionInterface;
8 use Drupal\Core\TypedData\TypedDataInterface; 8 use Drupal\Core\TypedData\TypedDataInterface;
9 use Drupal\Core\TypedData\TypedData; 9 use Drupal\Core\TypedData\TypedData;
10 use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
10 11
11 /** 12 /**
12 * A computed property for dates of date time field items. 13 * A computed property for dates of date time field items.
13 * 14 *
14 * Required settings (below the definition's 'settings' key) are: 15 * Required settings (below the definition's 'settings' key) are:
34 } 35 }
35 36
36 /** 37 /**
37 * {@inheritdoc} 38 * {@inheritdoc}
38 */ 39 */
39 public function getValue($langcode = NULL) { 40 public function getValue() {
40 if ($this->date !== NULL) { 41 if ($this->date !== NULL) {
41 return $this->date; 42 return $this->date;
42 } 43 }
43 44
44 /** @var \Drupal\Core\Field\FieldItemInterface $item */ 45 /** @var \Drupal\Core\Field\FieldItemInterface $item */
45 $item = $this->getParent(); 46 $item = $this->getParent();
46 $value = $item->{($this->definition->getSetting('date source'))}; 47 $value = $item->{($this->definition->getSetting('date source'))};
47 48
48 $datetime_type = $item->getFieldDefinition()->getSetting('datetime_type'); 49 $datetime_type = $item->getFieldDefinition()->getSetting('datetime_type');
49 $storage_format = $datetime_type === DateTimeItem::DATETIME_TYPE_DATE ? DATETIME_DATE_STORAGE_FORMAT : DATETIME_DATETIME_STORAGE_FORMAT; 50 $storage_format = $datetime_type === DateTimeItem::DATETIME_TYPE_DATE ? DateTimeItemInterface::DATE_STORAGE_FORMAT : DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
50 try { 51 try {
51 $date = DrupalDateTime::createFromFormat($storage_format, $value, DATETIME_STORAGE_TIMEZONE); 52 $date = DrupalDateTime::createFromFormat($storage_format, $value, DateTimeItemInterface::STORAGE_TIMEZONE);
52 if ($date instanceof DrupalDateTime && !$date->hasErrors()) { 53 if ($date instanceof DrupalDateTime && !$date->hasErrors()) {
53 $this->date = $date; 54 $this->date = $date;
54 // If the format did not include an explicit time portion, then the 55 // If the format did not include an explicit time portion, then the
55 // time will be set from the current time instead. For consistency, we 56 // time will be set from the current time instead. For consistency, we
56 // set the time to 12:00:00 UTC for date-only fields. This is used so 57 // set the time to 12:00:00 UTC for date-only fields. This is used so
57 // that the local date portion is the same, across nearly all time 58 // that the local date portion is the same, across nearly all time
58 // zones. 59 // zones.
59 // @see datetime_date_default_time() 60 // @see \Drupal\Component\Datetime\DateTimePlus::setDefaultDateTime()
60 // @see http://php.net/manual/en/datetime.createfromformat.php 61 // @see http://php.net/manual/datetime.createfromformat.php
61 // @todo Update comment and/or code per the chosen solution in
62 // https://www.drupal.org/node/2830094
63 if ($datetime_type === DateTimeItem::DATETIME_TYPE_DATE) { 62 if ($datetime_type === DateTimeItem::DATETIME_TYPE_DATE) {
64 $this->date->setTime(12, 0, 0); 63 $this->date->setDefaultDateTime();
65 } 64 }
66 } 65 }
67 } 66 }
68 catch (\Exception $e) { 67 catch (\Exception $e) {
69 // @todo Handle this. 68 // @todo Handle this.