/web/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php

https://gitlab.com/andecode/theme-spark · PHP · 139 lines · 78 code · 18 blank · 43 comment · 6 complexity · 7e03ecd4c8b922aca43956e6053e75ef MD5 · raw file

  1. <?php
  2. namespace Drupal\datetime\Plugin\Field\FieldType;
  3. use Drupal\Core\Field\FieldDefinitionInterface;
  4. use Drupal\Core\Field\FieldStorageDefinitionInterface;
  5. use Drupal\Core\Form\FormStateInterface;
  6. use Drupal\Core\TypedData\DataDefinition;
  7. use Drupal\Core\Field\FieldItemBase;
  8. /**
  9. * Plugin implementation of the 'datetime' field type.
  10. *
  11. * @FieldType(
  12. * id = "datetime",
  13. * label = @Translation("Date"),
  14. * description = @Translation("Create and store date values."),
  15. * default_widget = "datetime_default",
  16. * default_formatter = "datetime_default",
  17. * list_class = "\Drupal\datetime\Plugin\Field\FieldType\DateTimeFieldItemList",
  18. * constraints = {"DateTimeFormat" = {}}
  19. * )
  20. */
  21. class DateTimeItem extends FieldItemBase implements DateTimeItemInterface {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public static function defaultStorageSettings() {
  26. return [
  27. 'datetime_type' => 'datetime',
  28. ] + parent::defaultStorageSettings();
  29. }
  30. /**
  31. * Value for the 'datetime_type' setting: store only a date.
  32. */
  33. const DATETIME_TYPE_DATE = 'date';
  34. /**
  35. * Value for the 'datetime_type' setting: store a date and time.
  36. */
  37. const DATETIME_TYPE_DATETIME = 'datetime';
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
  42. $properties['value'] = DataDefinition::create('datetime_iso8601')
  43. ->setLabel(t('Date value'))
  44. ->setRequired(TRUE);
  45. $properties['date'] = DataDefinition::create('any')
  46. ->setLabel(t('Computed date'))
  47. ->setDescription(t('The computed DateTime object.'))
  48. ->setComputed(TRUE)
  49. ->setClass('\Drupal\datetime\DateTimeComputed')
  50. ->setSetting('date source', 'value');
  51. return $properties;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public static function schema(FieldStorageDefinitionInterface $field_definition) {
  57. return [
  58. 'columns' => [
  59. 'value' => [
  60. 'description' => 'The date value.',
  61. 'type' => 'varchar',
  62. 'length' => 20,
  63. ],
  64. ],
  65. 'indexes' => [
  66. 'value' => ['value'],
  67. ],
  68. ];
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
  74. $element = [];
  75. $element['datetime_type'] = [
  76. '#type' => 'select',
  77. '#title' => t('Date type'),
  78. '#description' => t('Choose the type of date to create.'),
  79. '#default_value' => $this->getSetting('datetime_type'),
  80. '#options' => [
  81. static::DATETIME_TYPE_DATETIME => t('Date and time'),
  82. static::DATETIME_TYPE_DATE => t('Date only'),
  83. ],
  84. '#disabled' => $has_data,
  85. ];
  86. return $element;
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
  92. $type = $field_definition->getSetting('datetime_type');
  93. // Just pick a date in the past year. No guidance is provided by this Field
  94. // type.
  95. $timestamp = REQUEST_TIME - mt_rand(0, 86400 * 365);
  96. if ($type == DateTimeItem::DATETIME_TYPE_DATE) {
  97. $values['value'] = gmdate(static::DATE_STORAGE_FORMAT, $timestamp);
  98. }
  99. else {
  100. $values['value'] = gmdate(static::DATETIME_STORAGE_FORMAT, $timestamp);
  101. }
  102. return $values;
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function isEmpty() {
  108. $value = $this->get('value')->getValue();
  109. return $value === NULL || $value === '';
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function onChange($property_name, $notify = TRUE) {
  115. // Enforce that the computed date is recalculated.
  116. if ($property_name == 'value') {
  117. $this->date = NULL;
  118. }
  119. parent::onChange($property_name, $notify);
  120. }
  121. }