class DateTimeItem
Same name and namespace in other branches
- 11.x core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php \Drupal\datetime\Plugin\Field\FieldType\DateTimeItem
- 10 core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php \Drupal\datetime\Plugin\Field\FieldType\DateTimeItem
- 8.9.x core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php \Drupal\datetime\Plugin\Field\FieldType\DateTimeItem
- main core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php \Drupal\datetime\Plugin\Field\FieldType\DateTimeItem
Plugin implementation of the 'datetime' field type.
Plugin annotation
@FieldType(
id = "datetime",
label = @Translation("Date"),
description = @Translation("Create and store date values."),
default_widget = "datetime_default",
default_formatter = "datetime_default",
list_class = "\Drupal\datetime\Plugin\Field\FieldType\DateTimeFieldItemList",
constraints = {"DateTimeFormat" = {}}
)
Hierarchy
- class \Drupal\Core\TypedData\TypedData implements \Drupal\Core\TypedData\TypedDataInterface, \Drupal\Component\Plugin\PluginInspectionInterface uses \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\TypedData\TypedDataTrait
- class \Drupal\Core\TypedData\Plugin\DataType\Map implements \Drupal\Core\TypedData\Plugin\DataType\IteratorAggregate, \Drupal\Core\TypedData\ComplexDataInterface extends \Drupal\Core\TypedData\TypedData
- class \Drupal\Core\Field\FieldItemBase implements \Drupal\Core\Field\FieldItemInterface extends \Drupal\Core\TypedData\Plugin\DataType\Map
- class \Drupal\datetime\Plugin\Field\FieldType\DateTimeItem implements \Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface extends \Drupal\Core\Field\FieldItemBase
- class \Drupal\Core\Field\FieldItemBase implements \Drupal\Core\Field\FieldItemInterface extends \Drupal\Core\TypedData\Plugin\DataType\Map
- class \Drupal\Core\TypedData\Plugin\DataType\Map implements \Drupal\Core\TypedData\Plugin\DataType\IteratorAggregate, \Drupal\Core\TypedData\ComplexDataInterface extends \Drupal\Core\TypedData\TypedData
Expanded class hierarchy of DateTimeItem
22 files declare their use of DateTimeItem
- Date.php in core/
modules/ datetime/ src/ Plugin/ views/ filter/ Date.php - Date.php in core/
modules/ datetime/ src/ Plugin/ views/ sort/ Date.php - Date.php in core/
modules/ datetime/ src/ Plugin/ views/ argument/ Date.php - DateFilterTest.php in core/
modules/ datetime/ tests/ src/ Functional/ DateFilterTest.php - DateRangeItem.php in core/
modules/ datetime_range/ src/ Plugin/ Field/ FieldType/ DateRangeItem.php
File
-
core/
modules/ datetime/ src/ Plugin/ Field/ FieldType/ DateTimeItem.php, line 25
Namespace
Drupal\datetime\Plugin\Field\FieldTypeView source
class DateTimeItem extends FieldItemBase implements DateTimeItemInterface {
/**
* {@inheritdoc}
*/
public static function defaultStorageSettings() {
return [
'datetime_type' => 'datetime',
] + parent::defaultStorageSettings();
}
/**
* 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';
/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('datetime_iso8601')->setLabel(new TranslatableMarkup('Date value'))
->setRequired(TRUE);
$properties['date'] = DataDefinition::create('any')->setLabel(new TranslatableMarkup('Computed date'))
->setDescription(new TranslatableMarkup('The computed DateTime object.'))
->setComputed(TRUE)
->setClass('\\Drupal\\datetime\\DateTimeComputed')
->setSetting('date source', 'value');
return $properties;
}
/**
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'description' => 'The date value.',
'type' => 'varchar',
'length' => 20,
],
],
'indexes' => [
'value' => [
'value',
],
],
];
}
/**
* {@inheritdoc}
*/
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$element = [];
$element['datetime_type'] = [
'#type' => 'select',
'#title' => $this->t('Date type'),
'#description' => $this->t('Choose the type of date to create.'),
'#default_value' => $this->getSetting('datetime_type'),
'#options' => [
static::DATETIME_TYPE_DATETIME => $this->t('Date and time'),
static::DATETIME_TYPE_DATE => $this->t('Date only'),
],
'#disabled' => $has_data,
];
return $element;
}
/**
* {@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(static::DATE_STORAGE_FORMAT, $timestamp);
}
else {
$values['value'] = gmdate(static::DATETIME_STORAGE_FORMAT, $timestamp);
}
return $values;
}
/**
* {@inheritdoc}
*/
public function isEmpty() {
$value = $this->get('value')
->getValue();
return $value === NULL || $value === '';
}
/**
* {@inheritdoc}
*/
public function onChange($property_name, $notify = TRUE) {
// Enforce that the computed date is recalculated.
if ($property_name == 'value') {
$this->date = NULL;
}
parent::onChange($property_name, $notify);
}
}
Members
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.