You are here

public static function DateTimeFieldItemList::processDefaultValue in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/datetime/src/Plugin/Field/FieldType/DateTimeFieldItemList.php \Drupal\datetime\Plugin\Field\FieldType\DateTimeFieldItemList::processDefaultValue()

Processes the default value before being applied.

Defined or configured default values of a field might need some processing in order to be a valid runtime value for the field type; e.g., a date field could process the defined value of 'NOW' to a valid date.

Parameters

array: The unprocessed default value defined for the field, as a numerically indexed array of items, each item being an array of property/value pairs.

\Drupal\Core\Entity\FieldableEntityInterface $entity: The entity for which the default value is generated.

\Drupal\Core\Field\FieldDefinitionInterface $definition: The definition of the field.

Return value

array The return default value for the field.

Overrides FieldItemList::processDefaultValue

File

core/modules/datetime/src/Plugin/Field/FieldType/DateTimeFieldItemList.php, line 96
Contains \Drupal\datetime\Plugin\Field\FieldType\DateTimeFieldItemList.

Class

DateTimeFieldItemList
Represents a configurable entity datetime field.

Namespace

Drupal\datetime\Plugin\Field\FieldType

Code

public static function processDefaultValue($default_value, FieldableEntityInterface $entity, FieldDefinitionInterface $definition) {
  $default_value = parent::processDefaultValue($default_value, $entity, $definition);
  if (isset($default_value[0]['default_date_type'])) {

    // A default value should be in the format and timezone used for date
    // storage.
    $date = new DrupalDateTime($default_value[0]['default_date'], DATETIME_STORAGE_TIMEZONE);
    $storage_format = $definition
      ->getSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE ? DATETIME_DATE_STORAGE_FORMAT : DATETIME_DATETIME_STORAGE_FORMAT;
    $value = $date
      ->format($storage_format);

    // 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.
    $default_value = array(
      array(
        'value' => $value,
        'date' => $date,
      ),
    );
  }
  return $default_value;
}