function DateTimeDatelistWidget::formElement

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

Overrides DateTimeWidgetBase::formElement

File

core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeDatelistWidget.php, line 34

Class

DateTimeDatelistWidget
Plugin implementation of the 'datetime_datelist' widget.

Namespace

Drupal\datetime\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);
    // Wrap all of the select elements with a fieldset.
    $element['#theme_wrappers'][] = 'fieldset';
    $date_order = $this->getSetting('date_order');
    if ($this->getFieldSetting('datetime_type') == 'datetime') {
        $time_type = $this->getSetting('time_type');
        $increment = $this->getSetting('increment');
    }
    else {
        $time_type = '';
        $increment = '';
    }
    // Set up the date part order array.
    $date_part_order = match ($date_order) {    'YMD' => [
            'year',
            'month',
            'day',
        ],
        'MDY' => [
            'month',
            'day',
            'year',
        ],
        'DMY' => [
            'day',
            'month',
            'year',
        ],
    
    };
    $date_part_order = match ($time_type) {    '24' => array_merge($date_part_order, [
            'hour',
            'minute',
        ]),
        '12' => array_merge($date_part_order, [
            'hour',
            'minute',
            'ampm',
        ]),
        default => $date_part_order,
    
    };
    $element['value'] = [
        '#type' => 'datelist',
        '#date_increment' => $increment,
        '#date_part_order' => $date_part_order,
    ] + $element['value'];
    return $element;
}

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