DateTimeItem.php 3 KB
Newer Older
1
2
3
4
<?php

/**
 * @file
5
 * Contains \Drupal\datetime\Plugin\Field\FieldType\DateTimeItem.
6
7
 */

8
namespace Drupal\datetime\Plugin\Field\FieldType;
9

10
use Drupal\Core\Field\FieldStorageDefinitionInterface;
11
use Drupal\Core\Form\FormStateInterface;
12
use Drupal\Core\TypedData\DataDefinition;
13
use Drupal\Core\Field\FieldItemBase;
14
15
16
17
18
19
20
21
22

/**
 * Plugin implementation of the 'datetime' field type.
 *
 * @FieldType(
 *   id = "datetime",
 *   label = @Translation("Date"),
 *   description = @Translation("Create and store date values."),
 *   default_widget = "datetime_default",
23
24
 *   default_formatter = "datetime_default",
 *   list_class = "\Drupal\datetime\Plugin\Field\FieldType\DateTimeFieldItemList"
25
26
 * )
 */
27
class DateTimeItem extends FieldItemBase {
28

29
30
31
32
33
34
35
36
37
  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return array(
      'datetime_type' => 'datetime',
    ) + parent::defaultSettings();
  }

38
39
40
41
42
43
44
45
46
47
  /**
   * 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';

48
49
50
  /**
   * {@inheritdoc}
   */
51
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
52
53
    $properties['value'] = DataDefinition::create('datetime_iso8601')
      ->setLabel(t('Date value'));
54

55
    $properties['date'] = DataDefinition::create('any')
56
57
58
59
60
      ->setLabel(t('Computed date'))
      ->setDescription(t('The computed DateTime object.'))
      ->setComputed(TRUE)
      ->setClass('\Drupal\datetime\DateTimeComputed')
      ->setSetting('date source', 'value');
61

62
    return $properties;
63
64
65
66
67
  }

  /**
   * {@inheritdoc}
   */
68
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    return array(
      'columns' => array(
        'value' => array(
          'description' => 'The date value.',
          'type' => 'varchar',
          'length' => 20,
          'not null' => FALSE,
        ),
      ),
      'indexes' => array(
        'value' => array('value'),
      ),
    );
  }

  /**
   * {@inheritdoc}
   */
87
  public function settingsForm(array &$form, FormStateInterface $form_state, $has_data) {
88
89
90
91
92
93
    $element = array();

    $element['datetime_type'] = array(
      '#type' => 'select',
      '#title' => t('Date type'),
      '#description' => t('Choose the type of date to create.'),
94
      '#default_value' => $this->getSetting('datetime_type'),
95
      '#options' => array(
96
97
        static::DATETIME_TYPE_DATETIME => t('Date and time'),
        static::DATETIME_TYPE_DATE => t('Date only'),
98
99
100
101
102
103
104
105
106
107
108
109
110
111
      ),
    );

    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function isEmpty() {
    $value = $this->get('value')->getValue();
    return $value === NULL || $value === '';
  }

112
113
114
115
116
117
118
119
120
121
122
123
  /**
   * {@inheritdoc}
   */
  public function onChange($property_name) {
    parent::onChange($property_name);

    // Enforce that the computed date is recalculated.
    if ($property_name == 'value') {
      $this->date = NULL;
    }
  }

124
}