function DateRangeDefaultWidget::formElement

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

Overrides DateRangeWidgetBase::formElement

File

core/modules/datetime_range/src/Plugin/Field/FieldWidget/DateRangeDefaultWidget.php, line 57

Class

DateRangeDefaultWidget
Plugin implementation of the 'daterange_default' widget.

Namespace

Drupal\datetime_range\Plugin\Field\FieldWidget

Code

public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $element = parent::formElement($items, $delta, $element, $form, $form_state);
    // Identify the type of date and time elements to use.
    switch ($this->getFieldSetting('datetime_type')) {
        case DateRangeItem::DATETIME_TYPE_DATE:
        case DateRangeItem::DATETIME_TYPE_ALLDAY:
            $date_type = 'date';
            $time_type = 'none';
            $date_format = $this->dateStorage
                ->load('html_date')
                ->getPattern();
            $time_format = '';
            break;
        default:
            $date_type = 'date';
            $time_type = 'time';
            $date_format = $this->dateStorage
                ->load('html_date')
                ->getPattern();
            $time_format = $this->dateStorage
                ->load('html_time')
                ->getPattern();
            break;
    }
    $element['value'] += [
        '#date_date_format' => $date_format,
        '#date_date_element' => $date_type,
        '#date_date_callbacks' => [],
        '#date_time_format' => $time_format,
        '#date_time_element' => $time_type,
        '#date_time_callbacks' => [],
    ];
    $element['end_value'] += [
        '#date_date_format' => $date_format,
        '#date_date_element' => $date_type,
        '#date_date_callbacks' => [],
        '#date_time_format' => $time_format,
        '#date_time_element' => $time_type,
        '#date_time_callbacks' => [],
    ];
    return $element;
}

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