function DateTimeItem::generateSampleValue

Overrides FieldItemBase::generateSampleValue

1 method overrides DateTimeItem::generateSampleValue()
DateRangeItem::generateSampleValue in core/modules/datetime_range/src/Plugin/Field/FieldType/DateRangeItem.php
Generates placeholder field values.

File

core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php, line 106

Class

DateTimeItem
Plugin implementation of the 'datetime' field type.

Namespace

Drupal\datetime\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.
  $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;
}

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