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@14: public function getValue() { 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@14: $storage_format = $datetime_type === DateTimeItem::DATETIME_TYPE_DATE ? DateTimeItemInterface::DATE_STORAGE_FORMAT : DateTimeItemInterface::DATETIME_STORAGE_FORMAT; Chris@0: try { Chris@14: $date = DrupalDateTime::createFromFormat($storage_format, $value, DateTimeItemInterface::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@14: // @see \Drupal\Component\Datetime\DateTimePlus::setDefaultDateTime() Chris@14: // @see http://php.net/manual/datetime.createfromformat.php Chris@0: if ($datetime_type === DateTimeItem::DATETIME_TYPE_DATE) { Chris@14: $this->date->setDefaultDateTime(); 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: }