'datetime', ] + parent::defaultStorageSettings(); } /** * Value for the 'datetime_type' setting: store only a date. */ const DATETIME_TYPE_DATE = 'date'; /** * Value for the 'datetime_type' setting: store a date and time. */ const DATETIME_TYPE_DATETIME = 'datetime'; /** * {@inheritdoc} */ public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { $properties['value'] = DataDefinition::create('datetime_iso8601') ->setLabel(t('Date value')) ->setRequired(TRUE); $properties['date'] = DataDefinition::create('any') ->setLabel(t('Computed date')) ->setDescription(t('The computed DateTime object.')) ->setComputed(TRUE) ->setClass('\Drupal\datetime\DateTimeComputed') ->setSetting('date source', 'value'); return $properties; } /** * {@inheritdoc} */ public static function schema(FieldStorageDefinitionInterface $field_definition) { return [ 'columns' => [ 'value' => [ 'description' => 'The date value.', 'type' => 'varchar', 'length' => 20, ], ], 'indexes' => [ 'value' => ['value'], ], ]; } /** * {@inheritdoc} */ public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) { $element = []; $element['datetime_type'] = [ '#type' => 'select', '#title' => t('Date type'), '#description' => t('Choose the type of date to create.'), '#default_value' => $this->getSetting('datetime_type'), '#options' => [ static::DATETIME_TYPE_DATETIME => t('Date and time'), static::DATETIME_TYPE_DATE => t('Date only'), ], '#disabled' => $has_data, ]; return $element; } /** * {@inheritdoc} */ public static function generateSampleValue(FieldDefinitionInterface $field_definition) { $type = $field_definition->getSetting('datetime_type'); // Just pick a date in the past year. No guidance is provided by this Field // type. $timestamp = REQUEST_TIME - mt_rand(0, 86400 * 365); if ($type == DateTimeItem::DATETIME_TYPE_DATE) { $values['value'] = gmdate(static::DATE_STORAGE_FORMAT, $timestamp); } else { $values['value'] = gmdate(static::DATETIME_STORAGE_FORMAT, $timestamp); } return $values; } /** * {@inheritdoc} */ public function isEmpty() { $value = $this->get('value')->getValue(); return $value === NULL || $value === ''; } /** * {@inheritdoc} */ public function onChange($property_name, $notify = TRUE) { // Enforce that the computed date is recalculated. if ($property_name == 'value') { $this->date = NULL; } parent::onChange($property_name, $notify); } }