DateTimeItem.php 3.64 KB
Newer Older
1 2
<?php

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 13 14 15 16 17 18

/**
 * Plugin implementation of the 'datetime' field type.
 *
 * @FieldType(
 *   id = "datetime",
 *   label = @Translation("Date"),
 *   description = @Translation("Create and store date values."),
 *   default_widget = "datetime_default",
19
 *   default_formatter = "datetime_default",
20 21
 *   list_class = "\Drupal\datetime\Plugin\Field\FieldType\DateTimeFieldItemList",
 *   constraints = {"DateTimeFormat" = {}}
22 23
 * )
 */
24
class DateTimeItem extends FieldItemBase implements DateTimeItemInterface {
25

26 27 28
  /**
   * {@inheritdoc}
   */
29
  public static function defaultStorageSettings() {
30
    return [
31
      'datetime_type' => 'datetime',
32
    ] + parent::defaultStorageSettings();
33 34
  }

35 36 37 38 39 40 41 42 43 44
  /**
   * 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';

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

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

60
    return $properties;
61 62 63 64 65
  }

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

  /**
   * {@inheritdoc}
   */
84
  public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
85
    $element = [];
86

87
    $element['datetime_type'] = [
88 89 90
      '#type' => 'select',
      '#title' => t('Date type'),
      '#description' => t('Choose the type of date to create.'),
91
      '#default_value' => $this->getSetting('datetime_type'),
92
      '#options' => [
93 94
        static::DATETIME_TYPE_DATETIME => t('Date and time'),
        static::DATETIME_TYPE_DATE => t('Date only'),
95
      ],
96
      '#disabled' => $has_data,
97
    ];
98 99 100 101

    return $element;
  }

102 103 104 105 106 107 108 109
  /**
   * {@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.
110
    $timestamp = REQUEST_TIME - mt_rand(0, 86400 * 365);
111
    if ($type == DateTimeItem::DATETIME_TYPE_DATE) {
112
      $values['value'] = gmdate(static::DATE_STORAGE_FORMAT, $timestamp);
113 114
    }
    else {
115
      $values['value'] = gmdate(static::DATETIME_STORAGE_FORMAT, $timestamp);
116 117 118 119
    }
    return $values;
  }

120 121 122 123 124 125 126 127
  /**
   * {@inheritdoc}
   */
  public function isEmpty() {
    $value = $this->get('value')->getValue();
    return $value === NULL || $value === '';
  }

128 129 130
  /**
   * {@inheritdoc}
   */
131
  public function onChange($property_name, $notify = TRUE) {
132 133 134 135
    // Enforce that the computed date is recalculated.
    if ($property_name == 'value') {
      $this->date = NULL;
    }
136
    parent::onChange($property_name, $notify);
137 138
  }

139
}