function DateTimeItem::generateSampleValue

Same name in other branches
  1. 9 core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php \Drupal\datetime\Plugin\Field\FieldType\DateTimeItem::generateSampleValue()
  2. 8.9.x core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php \Drupal\datetime\Plugin\Field\FieldType\DateTimeItem::generateSampleValue()
  3. 10 core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php \Drupal\datetime\Plugin\Field\FieldType\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 111

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