function DateRangeItem::generateSampleValue

Overrides DateTimeItem::generateSampleValue

File

core/modules/datetime_range/src/Plugin/Field/FieldType/DateRangeItem.php, line 98

Class

DateRangeItem
Plugin implementation of the 'daterange' field type.

Namespace

Drupal\datetime_range\Plugin\Field\FieldType

Code

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.
  $start = \Drupal::time()->getRequestTime() - 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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.