DateTimeItem.php 3.69 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\FieldStorageDefinitionInterface;
12
use Drupal\Core\Form\FormStateInterface;
13
use Drupal\Core\TypedData\DataDefinition;
14
use Drupal\Core\Field\FieldItemBase;
15 16 17 18 19 20 21 22 23

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

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

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

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

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

64
    return $properties;
65 66 67 68 69
  }

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

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

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

    return $element;
  }

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

124 125 126 127 128 129 130 131
  /**
   * {@inheritdoc}
   */
  public function isEmpty() {
    $value = $this->get('value')->getValue();
    return $value === NULL || $value === '';
  }

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

143
}