function TimestampDatetimeWidget::formElement

Same name in other branches
  1. 9 core/lib/Drupal/Core/Datetime/Plugin/Field/FieldWidget/TimestampDatetimeWidget.php \Drupal\Core\Datetime\Plugin\Field\FieldWidget\TimestampDatetimeWidget::formElement()
  2. 8.9.x core/lib/Drupal/Core/Datetime/Plugin/Field/FieldWidget/TimestampDatetimeWidget.php \Drupal\Core\Datetime\Plugin\Field\FieldWidget\TimestampDatetimeWidget::formElement()
  3. 11.x core/lib/Drupal/Core/Datetime/Plugin/Field/FieldWidget/TimestampDatetimeWidget.php \Drupal\Core\Datetime\Plugin\Field\FieldWidget\TimestampDatetimeWidget::formElement()

Overrides WidgetInterface::formElement

File

core/lib/Drupal/Core/Datetime/Plugin/Field/FieldWidget/TimestampDatetimeWidget.php, line 28

Class

TimestampDatetimeWidget
Plugin implementation of the 'datetime timestamp' widget.

Namespace

Drupal\Core\Datetime\Plugin\Field\FieldWidget

Code

public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $default_value = isset($items[$delta]->value) ? DrupalDateTime::createFromTimestamp($items[$delta]->value) : '';
    $element['value'] = $element + [
        '#type' => 'datetime',
        '#default_value' => $default_value,
        '#date_year_range' => '1902:2037',
    ];
    $element['value']['#description'] = $element['#description'] !== '' ? $element['#description'] : $this->t('Leave blank to use the time of form submission.');
    return $element;
}

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