DateRangeItem.php in Drupal 9
File
core/modules/datetime_range/src/Plugin/Field/FieldType/DateRangeItem.php
View source
<?php
namespace Drupal\datetime_range\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\datetime\DateTimeComputed;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
class DateRangeItem extends DateTimeItem {
const DATETIME_TYPE_ALLDAY = 'allday';
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('datetime_iso8601')
->setLabel(t('Start date value'))
->setRequired(TRUE);
$properties['start_date'] = DataDefinition::create('any')
->setLabel(t('Computed start date'))
->setDescription(t('The computed start DateTime object.'))
->setComputed(TRUE)
->setClass(DateTimeComputed::class)
->setSetting('date source', 'value');
$properties['end_value'] = DataDefinition::create('datetime_iso8601')
->setLabel(t('End date value'))
->setRequired(TRUE);
$properties['end_date'] = DataDefinition::create('any')
->setLabel(t('Computed end date'))
->setDescription(t('The computed end DateTime object.'))
->setComputed(TRUE)
->setClass(DateTimeComputed::class)
->setSetting('date source', 'end_value');
return $properties;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
$schema = parent::schema($field_definition);
$schema['columns']['value']['description'] = 'The start date value.';
$schema['columns']['end_value'] = [
'description' => 'The end date value.',
] + $schema['columns']['value'];
$schema['indexes']['end_value'] = [
'end_value',
];
return $schema;
}
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$element = parent::storageSettingsForm($form, $form_state, $has_data);
$element['datetime_type']['#options'][static::DATETIME_TYPE_ALLDAY] = $this
->t('All Day');
return $element;
}
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$type = $field_definition
->getSetting('datetime_type');
$start = REQUEST_TIME - mt_rand(0, 86400 * 365) - 86400;
$end = $start + 86400;
if ($type == static::DATETIME_TYPE_DATETIME) {
$values['value'] = gmdate(DateTimeItemInterface::DATETIME_STORAGE_FORMAT, $start);
$values['end_value'] = gmdate(DateTimeItemInterface::DATETIME_STORAGE_FORMAT, $end);
}
else {
$values['value'] = gmdate(DateTimeItemInterface::DATE_STORAGE_FORMAT, $start);
$values['end_value'] = gmdate(DateTimeItemInterface::DATE_STORAGE_FORMAT, $end);
}
return $values;
}
public function isEmpty() {
$start_value = $this
->get('value')
->getValue();
$end_value = $this
->get('end_value')
->getValue();
return ($start_value === NULL || $start_value === '') && ($end_value === NULL || $end_value === '');
}
public function onChange($property_name, $notify = TRUE) {
if ($property_name == 'value') {
$this->start_date = NULL;
}
elseif ($property_name == 'end_value') {
$this->end_date = NULL;
}
parent::onChange($property_name, $notify);
}
}