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