function DateRangeFieldItemList::processDefaultValue
Same name in other branches
- 8.9.x core/modules/datetime_range/src/Plugin/Field/FieldType/DateRangeFieldItemList.php \Drupal\datetime_range\Plugin\Field\FieldType\DateRangeFieldItemList::processDefaultValue()
- 10 core/modules/datetime_range/src/Plugin/Field/FieldType/DateRangeFieldItemList.php \Drupal\datetime_range\Plugin\Field\FieldType\DateRangeFieldItemList::processDefaultValue()
- 11.x core/modules/datetime_range/src/Plugin/Field/FieldType/DateRangeFieldItemList.php \Drupal\datetime_range\Plugin\Field\FieldType\DateRangeFieldItemList::processDefaultValue()
Overrides DateTimeFieldItemList::processDefaultValue
File
-
core/
modules/ datetime_range/ src/ Plugin/ Field/ FieldType/ DateRangeFieldItemList.php, line 96
Class
- DateRangeFieldItemList
- Represents a configurable entity daterange field.
Namespace
Drupal\datetime_range\Plugin\Field\FieldTypeCode
public static function processDefaultValue($default_value, FieldableEntityInterface $entity, FieldDefinitionInterface $definition) {
// Explicitly call the base class so that we can get the default value
// types.
$default_value = FieldItemList::processDefaultValue($default_value, $entity, $definition);
// Allow either the start or end date to have a default, but not require
// defaults for both.
if (!empty($default_value[0]['default_date_type']) || !empty($default_value[0]['default_end_date_type'])) {
// A default value should be in the format and timezone used for date
// storage. All-day ranges are stored the same as date+time ranges. We
// only provide a default value for the first item, as do all fields.
// Otherwise, there is no way to clear out unwanted values on multiple
// value fields.
$storage_format = $definition->getSetting('datetime_type') == DateRangeItem::DATETIME_TYPE_DATE ? DateTimeItemInterface::DATE_STORAGE_FORMAT : DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
$default_values = [
[],
];
if (!empty($default_value[0]['default_date_type'])) {
$start_date = new DrupalDateTime($default_value[0]['default_date'], DateTimeItemInterface::STORAGE_TIMEZONE);
$start_value = $start_date->format($storage_format);
$default_values[0]['value'] = $start_value;
$default_values[0]['start_date'] = $start_date;
}
if (!empty($default_value[0]['default_end_date_type'])) {
$end_date = new DrupalDateTime($default_value[0]['default_end_date'], DateTimeItemInterface::STORAGE_TIMEZONE);
$end_value = $end_date->format($storage_format);
$default_values[0]['end_value'] = $end_value;
$default_values[0]['end_date'] = $end_date;
}
$default_value = $default_values;
}
return $default_value;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.