Chris@0: getSetting('date source')) { Chris@0: throw new \InvalidArgumentException("The definition's 'date source' key has to specify the name of the date property to be computed."); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getValue($langcode = NULL) { Chris@0: if ($this->date !== NULL) { Chris@0: return $this->date; Chris@0: } Chris@0: Chris@0: /** @var \Drupal\Core\Field\FieldItemInterface $item */ Chris@0: $item = $this->getParent(); Chris@0: $value = $item->{($this->definition->getSetting('date source'))}; Chris@0: Chris@0: $datetime_type = $item->getFieldDefinition()->getSetting('datetime_type'); Chris@0: $storage_format = $datetime_type === DateTimeItem::DATETIME_TYPE_DATE ? DATETIME_DATE_STORAGE_FORMAT : DATETIME_DATETIME_STORAGE_FORMAT; Chris@0: try { Chris@0: $date = DrupalDateTime::createFromFormat($storage_format, $value, DATETIME_STORAGE_TIMEZONE); Chris@0: if ($date instanceof DrupalDateTime && !$date->hasErrors()) { Chris@0: $this->date = $date; Chris@0: // If the format did not include an explicit time portion, then the Chris@0: // time will be set from the current time instead. For consistency, we Chris@0: // set the time to 12:00:00 UTC for date-only fields. This is used so Chris@0: // that the local date portion is the same, across nearly all time Chris@0: // zones. Chris@0: // @see datetime_date_default_time() Chris@0: // @see http://php.net/manual/en/datetime.createfromformat.php Chris@0: // @todo Update comment and/or code per the chosen solution in Chris@0: // https://www.drupal.org/node/2830094 Chris@0: if ($datetime_type === DateTimeItem::DATETIME_TYPE_DATE) { Chris@0: $this->date->setTime(12, 0, 0); Chris@0: } Chris@0: } Chris@0: } Chris@0: catch (\Exception $e) { Chris@0: // @todo Handle this. Chris@0: } Chris@0: return $this->date; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setValue($value, $notify = TRUE) { Chris@0: $this->date = $value; Chris@0: // Notify the parent of any changes. Chris@0: if ($notify && isset($this->parent)) { Chris@0: $this->parent->onChange($this->name); Chris@0: } Chris@0: } Chris@0: Chris@0: }