You are here

public function DateDayComputed::getValue in Date time day 8

Gets the data value.

Return value

mixed The data value.

Overrides TypedData::getValue

File

src/DateDayComputed.php, line 39

Class

DateDayComputed
A computed property for date of date day field items.

Namespace

Drupal\date_time_day

Code

public function getValue($langcode = NULL) {
  if ($this->date !== NULL) {
    return $this->date;
  }

  /** @var \Drupal\Core\Field\FieldItemInterface $item */
  $item = $this
    ->getParent();
  $value = $item->{$this->definition
    ->getSetting('date source')};
  $storage_format = DateTimeItemInterface::DATE_STORAGE_FORMAT;
  try {
    $date = DrupalDateTime::createFromFormat($storage_format, $value, DateTimeItemInterface::STORAGE_TIMEZONE);
    if ($date instanceof DrupalDateTime && !$date
      ->hasErrors()) {
      $this->date = $date;

      // The format did not include an explicit time portion, then the
      // time will be set from the current time instead. For consistency, we
      // set the time to 12:00:00 UTC for date-only fields. This is used so
      // that the local date portion is the same, across nearly all time
      // zones.
      // @see http://php.net/manual/en/datetime.createfromformat.php
      $this->date
        ->setDefaultDateTime();
    }
  } catch (\Exception $e) {

    // @todo Handle this.
  }
  return $this->date;
}