function DateTimeWidgetBase::formElement

Same name in other branches
  1. 9 core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeWidgetBase.php \Drupal\datetime\Plugin\Field\FieldWidget\DateTimeWidgetBase::formElement()
  2. 8.9.x core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeWidgetBase.php \Drupal\datetime\Plugin\Field\FieldWidget\DateTimeWidgetBase::formElement()
  3. 11.x core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeWidgetBase.php \Drupal\datetime\Plugin\Field\FieldWidget\DateTimeWidgetBase::formElement()

Overrides WidgetInterface::formElement

3 calls to DateTimeWidgetBase::formElement()
DateRangeWidgetBase::formElement in core/modules/datetime_range/src/Plugin/Field/FieldWidget/DateRangeWidgetBase.php
Returns the form for a single field widget.
DateTimeDatelistWidget::formElement in core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeDatelistWidget.php
Returns the form for a single field widget.
DateTimeDefaultWidget::formElement in core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeDefaultWidget.php
Returns the form for a single field widget.
3 methods override DateTimeWidgetBase::formElement()
DateRangeWidgetBase::formElement in core/modules/datetime_range/src/Plugin/Field/FieldWidget/DateRangeWidgetBase.php
Returns the form for a single field widget.
DateTimeDatelistWidget::formElement in core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeDatelistWidget.php
Returns the form for a single field widget.
DateTimeDefaultWidget::formElement in core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeDefaultWidget.php
Returns the form for a single field widget.

File

core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeWidgetBase.php, line 20

Class

DateTimeWidgetBase
Base class for the 'datetime_*' widgets.

Namespace

Drupal\datetime\Plugin\Field\FieldWidget

Code

public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $element['value'] = [
        '#type' => 'datetime',
        '#default_value' => NULL,
        '#date_increment' => 1,
        '#date_timezone' => date_default_timezone_get(),
        '#required' => $element['#required'],
    ];
    if ($this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) {
        // A date-only field should have no timezone conversion performed, so
        // use the same timezone as for storage.
        $element['value']['#date_timezone'] = DateTimeItemInterface::STORAGE_TIMEZONE;
    }
    if ($items[$delta]->date) {
        $element['value']['#default_value'] = $this->createDefaultValue($items[$delta]->date, $element['value']['#date_timezone']);
    }
    return $element;
}

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