You are here

public function DoubleField::createDate in Double Field 8.3

Same name and namespace in other branches
  1. 4.x src/Plugin/Field/FieldType/DoubleField.php \Drupal\double_field\Plugin\Field\FieldType\DoubleField::createDate()

Creates date object for a given subfield using storage timezone.

File

src/Plugin/Field/FieldType/DoubleField.php, line 756

Class

DoubleField
Plugin implementation of the 'double_field' field type.

Namespace

Drupal\double_field\Plugin\Field\FieldType

Code

public function createDate($subfield) {
  if ($this->{$subfield}) {
    $is_date_only = $this
      ->getSetting('storage')[$subfield]['datetime_type'] == 'date';
    $format = $is_date_only ? static::DATETIME_DATE_STORAGE_FORMAT : static::DATETIME_DATETIME_STORAGE_FORMAT;
    $date = DrupalDateTime::createFromFormat($format, $this->{$subfield}, static::DATETIME_STORAGE_TIMEZONE);
    if ($is_date_only) {
      $date
        ->setDefaultDateTime();
    }
    return $date;
  }
}