You are here

public static function SmartDateFieldItemList::processDefaultValue in Smart Date 3.2.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldType/SmartDateFieldItemList.php \Drupal\smart_date\Plugin\Field\FieldType\SmartDateFieldItemList::processDefaultValue()
  2. 8 src/Plugin/Field/FieldType/SmartDateFieldItemList.php \Drupal\smart_date\Plugin\Field\FieldType\SmartDateFieldItemList::processDefaultValue()
  3. 3.x src/Plugin/Field/FieldType/SmartDateFieldItemList.php \Drupal\smart_date\Plugin\Field\FieldType\SmartDateFieldItemList::processDefaultValue()
  4. 3.0.x src/Plugin/Field/FieldType/SmartDateFieldItemList.php \Drupal\smart_date\Plugin\Field\FieldType\SmartDateFieldItemList::processDefaultValue()
  5. 3.1.x src/Plugin/Field/FieldType/SmartDateFieldItemList.php \Drupal\smart_date\Plugin\Field\FieldType\SmartDateFieldItemList::processDefaultValue()
  6. 3.3.x src/Plugin/Field/FieldType/SmartDateFieldItemList.php \Drupal\smart_date\Plugin\Field\FieldType\SmartDateFieldItemList::processDefaultValue()
  7. 3.4.x src/Plugin/Field/FieldType/SmartDateFieldItemList.php \Drupal\smart_date\Plugin\Field\FieldType\SmartDateFieldItemList::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 $default_value: 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 DateTimeFieldItemList::processDefaultValue

File

src/Plugin/Field/FieldType/SmartDateFieldItemList.php, line 128

Class

SmartDateFieldItemList
Represents a configurable entity smartdate field.

Namespace

Drupal\smart_date\Plugin\Field\FieldType

Code

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);

  // No default set, so nothing to do.
  if (empty($default_value[0]['default_date_type'])) {
    return $default_value;
  }

  // A default date+time value should be in the format and timezone used
  // for date storage.
  $date = new DrupalDateTime($default_value[0]['default_date'], DateTimeItemInterface::STORAGE_TIMEZONE);
  $format = DateTimeItemInterface::DATETIME_STORAGE_FORMAT;

  // If using 'next_hour' for 'default_date_type', do custom processing.
  if ($default_value[0]['default_date_type'] == 'next_hour') {
    $date
      ->modify('+1 hour');

    // After conversion to timestamp, we round up, so offset for this.
    $min = (int) $date
      ->format('i') + 1;
    $date
      ->modify('-' . $min . ' minutes');
  }
  $value = $date
    ->getTimestamp();

  // Round up to the next minute.
  $second = $date
    ->format("s");
  if ($second > 0) {
    $value += 60 - $second;
  }

  // Calculate the end value.
  $duration = (int) $default_value[0]['default_duration'];
  $end_value = $value + $duration * 60;
  $default_value = [
    [
      'value' => $value,
      'end_value' => $end_value,
      'date' => $date,
    ],
  ];
  return $default_value;
}