3 namespace Drupal\datetime_range\Plugin\Field\FieldType;
5 use Drupal\Core\Field\FieldDefinitionInterface;
6 use Drupal\Core\Field\FieldStorageDefinitionInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\TypedData\DataDefinition;
9 use Drupal\datetime\DateTimeComputed;
10 use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
13 * Plugin implementation of the 'daterange' field type.
17 * label = @Translation("Date range"),
18 * description = @Translation("Create and store date ranges."),
19 * default_widget = "daterange_default",
20 * default_formatter = "daterange_default",
21 * list_class = "\Drupal\datetime_range\Plugin\Field\FieldType\DateRangeFieldItemList"
24 class DateRangeItem extends DateTimeItem {
27 * Value for the 'datetime_type' setting: store a date and time.
29 const DATETIME_TYPE_ALLDAY = 'allday';
34 public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
35 $properties['value'] = DataDefinition::create('datetime_iso8601')
36 ->setLabel(t('Start date value'))
39 $properties['start_date'] = DataDefinition::create('any')
40 ->setLabel(t('Computed start date'))
41 ->setDescription(t('The computed start DateTime object.'))
43 ->setClass(DateTimeComputed::class)
44 ->setSetting('date source', 'value');
46 $properties['end_value'] = DataDefinition::create('datetime_iso8601')
47 ->setLabel(t('End date value'))
50 $properties['end_date'] = DataDefinition::create('any')
51 ->setLabel(t('Computed end date'))
52 ->setDescription(t('The computed end DateTime object.'))
54 ->setClass(DateTimeComputed::class)
55 ->setSetting('date source', 'end_value');
63 public static function schema(FieldStorageDefinitionInterface $field_definition) {
64 $schema = parent::schema($field_definition);
66 $schema['columns']['value']['description'] = 'The start date value.';
68 $schema['columns']['end_value'] = [
69 'description' => 'The end date value.',
70 ] + $schema['columns']['value'];
72 $schema['indexes']['end_value'] = ['end_value'];
80 public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
81 $element = parent::storageSettingsForm($form, $form_state, $has_data);
83 $element['datetime_type']['#options'][static::DATETIME_TYPE_ALLDAY] = $this->t('All Day');
91 public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
92 $type = $field_definition->getSetting('datetime_type');
94 // Just pick a date in the past year. No guidance is provided by this Field
96 $start = REQUEST_TIME - mt_rand(0, 86400 * 365) - 86400;
97 $end = $start + 86400;
98 if ($type == static::DATETIME_TYPE_DATETIME) {
99 $values['value'] = gmdate(DATETIME_DATETIME_STORAGE_FORMAT, $start);
100 $values['end_value'] = gmdate(DATETIME_DATETIME_STORAGE_FORMAT, $end);
103 $values['value'] = gmdate(DATETIME_DATE_STORAGE_FORMAT, $start);
104 $values['end_value'] = gmdate(DATETIME_DATE_STORAGE_FORMAT, $end);
112 public function isEmpty() {
113 $start_value = $this->get('value')->getValue();
114 $end_value = $this->get('end_value')->getValue();
115 return ($start_value === NULL || $start_value === '') && ($end_value === NULL || $end_value === '');
121 public function onChange($property_name, $notify = TRUE) {
122 // Enforce that the computed date is recalculated.
123 if ($property_name == 'value') {
124 $this->start_date = NULL;
126 elseif ($property_name == 'end_value') {
127 $this->end_date = NULL;
129 parent::onChange($property_name, $notify);