function DateRangeDatelistWidget::formElement

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

Overrides DateRangeWidgetBase::formElement

File

core/modules/datetime_range/src/Plugin/Field/FieldWidget/DateRangeDatelistWidget.php, line 36

Class

DateRangeDatelistWidget
Plugin implementation of the 'daterange_datelist' 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);
    $date_order = $this->getSetting('date_order');
    if ($this->getFieldSetting('datetime_type') == DateRangeItem::DATETIME_TYPE_DATETIME) {
        $time_type = $this->getSetting('time_type');
        $increment = $this->getSetting('increment');
    }
    else {
        $time_type = '';
        $increment = '';
    }
    // Set up the date part order array.
    switch ($date_order) {
        default:
        case 'YMD':
            $date_part_order = [
                'year',
                'month',
                'day',
            ];
            break;
        case 'MDY':
            $date_part_order = [
                'month',
                'day',
                'year',
            ];
            break;
        case 'DMY':
            $date_part_order = [
                'day',
                'month',
                'year',
            ];
            break;
    }
    switch ($time_type) {
        case '24':
            $date_part_order = array_merge($date_part_order, [
                'hour',
                'minute',
            ]);
            break;
        case '12':
            $date_part_order = array_merge($date_part_order, [
                'hour',
                'minute',
                'ampm',
            ]);
            break;
        case 'none':
            break;
    }
    $element['value'] = [
        '#type' => 'datelist',
        '#date_increment' => $increment,
        '#date_part_order' => $date_part_order,
    ] + $element['value'];
    $element['end_value'] = [
        '#type' => 'datelist',
        '#date_increment' => $increment,
        '#date_part_order' => $date_part_order,
    ] + $element['end_value'];
    return $element;
}

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