function TimestampDatetimeWidget::formElement

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Datetime/Plugin/Field/FieldWidget/TimestampDatetimeWidget.php \Drupal\Core\Datetime\Plugin\Field\FieldWidget\TimestampDatetimeWidget::formElement()
  2. 10 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 29

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) {
    $date_format = DateFormat::load('html_date')->getPattern();
    $time_format = DateFormat::load('html_time')->getPattern();
    $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('Format: %format. Leave blank to use the time of form submission.', [
        '%format' => Datetime::formatExample($date_format . ' ' . $time_format),
    ]);
    return $element;
}

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