330782b0d69460701cb3e0bcfe259ed289e7fb10
[yaffs-website] / web / core / modules / datetime / src / Plugin / Field / FieldType / DateTimeItem.php
1 <?php
2
3 namespace Drupal\datetime\Plugin\Field\FieldType;
4
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\Core\Field\FieldItemBase;
10
11 /**
12  * Plugin implementation of the 'datetime' field type.
13  *
14  * @FieldType(
15  *   id = "datetime",
16  *   label = @Translation("Date"),
17  *   description = @Translation("Create and store date values."),
18  *   default_widget = "datetime_default",
19  *   default_formatter = "datetime_default",
20  *   list_class = "\Drupal\datetime\Plugin\Field\FieldType\DateTimeFieldItemList",
21  *   constraints = {"DateTimeFormat" = {}}
22  * )
23  */
24 class DateTimeItem extends FieldItemBase {
25
26   /**
27    * {@inheritdoc}
28    */
29   public static function defaultStorageSettings() {
30     return [
31       'datetime_type' => 'datetime',
32     ] + parent::defaultStorageSettings();
33   }
34
35   /**
36    * Value for the 'datetime_type' setting: store only a date.
37    */
38   const DATETIME_TYPE_DATE = 'date';
39
40   /**
41    * Value for the 'datetime_type' setting: store a date and time.
42    */
43   const DATETIME_TYPE_DATETIME = 'datetime';
44
45   /**
46    * {@inheritdoc}
47    */
48   public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
49     $properties['value'] = DataDefinition::create('datetime_iso8601')
50       ->setLabel(t('Date value'))
51       ->setRequired(TRUE);
52
53     $properties['date'] = DataDefinition::create('any')
54       ->setLabel(t('Computed date'))
55       ->setDescription(t('The computed DateTime object.'))
56       ->setComputed(TRUE)
57       ->setClass('\Drupal\datetime\DateTimeComputed')
58       ->setSetting('date source', 'value');
59
60     return $properties;
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public static function schema(FieldStorageDefinitionInterface $field_definition) {
67     return [
68       'columns' => [
69         'value' => [
70           'description' => 'The date value.',
71           'type' => 'varchar',
72           'length' => 20,
73         ],
74       ],
75       'indexes' => [
76         'value' => ['value'],
77       ],
78     ];
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
85     $element = [];
86
87     $element['datetime_type'] = [
88       '#type' => 'select',
89       '#title' => t('Date type'),
90       '#description' => t('Choose the type of date to create.'),
91       '#default_value' => $this->getSetting('datetime_type'),
92       '#options' => [
93         static::DATETIME_TYPE_DATETIME => t('Date and time'),
94         static::DATETIME_TYPE_DATE => t('Date only'),
95       ],
96       '#disabled' => $has_data,
97     ];
98
99     return $element;
100   }
101
102   /**
103    * {@inheritdoc}
104    */
105   public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
106     $type = $field_definition->getSetting('datetime_type');
107
108     // Just pick a date in the past year. No guidance is provided by this Field
109     // type.
110     $timestamp = REQUEST_TIME - mt_rand(0, 86400 * 365);
111     if ($type == DateTimeItem::DATETIME_TYPE_DATE) {
112       $values['value'] = gmdate(DATETIME_DATE_STORAGE_FORMAT, $timestamp);
113     }
114     else {
115       $values['value'] = gmdate(DATETIME_DATETIME_STORAGE_FORMAT, $timestamp);
116     }
117     return $values;
118   }
119
120   /**
121    * {@inheritdoc}
122    */
123   public function isEmpty() {
124     $value = $this->get('value')->getValue();
125     return $value === NULL || $value === '';
126   }
127
128   /**
129    * {@inheritdoc}
130    */
131   public function onChange($property_name, $notify = TRUE) {
132     // Enforce that the computed date is recalculated.
133     if ($property_name == 'value') {
134       $this->date = NULL;
135     }
136     parent::onChange($property_name, $notify);
137   }
138
139 }