DateTimeItem.php 3.41 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\FieldDefinitionInterface;
11
use Drupal\Core\Field\PrepareCacheInterface;
12
use Drupal\Core\TypedData\DataDefinition;
13
use Drupal\Core\Field\FieldItemBase;
14 15 16 17 18 19 20 21 22 23 24 25

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

32 33 34 35 36 37 38 39 40 41
  /**
   * 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';

42 43 44
  /**
   * {@inheritdoc}
   */
45 46 47
  public static function propertyDefinitions(FieldDefinitionInterface $field_definition) {
    $properties['value'] = DataDefinition::create('datetime_iso8601')
      ->setLabel(t('Date value'));
48

49 50 51 52 53 54
    $properties['date'] = DataDefinition::create('datetime_computed')
      ->setLabel(t('Computed date'))
      ->setDescription(t('The computed DateTime object.'))
      ->setComputed(TRUE)
      ->setClass('\Drupal\datetime\DateTimeComputed')
      ->setSetting('date source', 'value');
55

56
    return $properties;
57 58 59 60 61
  }

  /**
   * {@inheritdoc}
   */
62
  public static function schema(FieldDefinitionInterface $field_definition) {
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    return array(
      'columns' => array(
        'value' => array(
          'description' => 'The date value.',
          'type' => 'varchar',
          'length' => 20,
          'not null' => FALSE,
        ),
      ),
      'indexes' => array(
        'value' => array('value'),
      ),
    );
  }

  /**
   * {@inheritdoc}
   */
81
  public function settingsForm(array $form, array &$form_state, $has_data) {
82 83 84 85 86 87
    $element = array();

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

    return $element;
  }

  /**
   * {@inheritdoc}
   */
101 102
  public function getCacheData() {
    $data = $this->getValue();
103 104 105 106
    // The function generates a Date object for each field early so that it is
    // cached in the field cache. This avoids the need to generate the object
    // later. The date will be retrieved in UTC, the local timezone adjustment
    // must be made in real time, based on the preferences of the site and user.
107 108
    if (!empty($data['value'])) {
      $data['date'] = $this->date;
109
    }
110
    return $data;
111 112 113 114 115 116 117 118 119 120
  }

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

121 122 123 124 125 126 127 128 129 130 131 132
  /**
   * {@inheritdoc}
   */
  public function onChange($property_name) {
    parent::onChange($property_name);

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

133
}