You are here

public function DateTimeDayComputed::getValue in Date time day 8

Gets the data value.

Return value

mixed The data value.

Overrides TypedData::getValue

File

src/DateTimeDayComputed.php, line 40

Class

DateTimeDayComputed
A computed property for times of date time 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')};
  $datetime_type = $item
    ->getFieldDefinition()
    ->getSetting('time_type');
  $storage_format = $datetime_type === DateTimeDayItem::DATEDAY_TIME_DEFAULT_TYPE_FORMAT ? DateTimeDayItem::DATE_TIME_DAY_H_I_FORMAT_STORAGE_FORMAT : DateTimeDayItem::DATE_TIME_DAY_H_I_S_FORMAT_STORAGE_FORMAT;

  // Fix time with seconds in incorrect widget.
  if ($datetime_type === DateTimeDayItem::DATEDAY_TIME_TYPE_SECONDS_FORMAT && strlen($value) === 5) {
    $value = "{$value}:00";
  }
  try {
    $date = DrupalDateTime::createFromFormat($storage_format, $value, DateTimeItemInterface::STORAGE_TIMEZONE);
    if ($date instanceof DrupalDateTime && !$date
      ->hasErrors()) {
      $this->date = $date;
    }
  } catch (\Exception $e) {

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