function WidgetBase::formSingleElement

Same name in other branches
  1. 9 core/lib/Drupal/Core/Field/WidgetBase.php \Drupal\Core\Field\WidgetBase::formSingleElement()
  2. 8.9.x core/lib/Drupal/Core/Field/WidgetBase.php \Drupal\Core\Field\WidgetBase::formSingleElement()
  3. 11.x core/lib/Drupal/Core/Field/WidgetBase.php \Drupal\Core\Field\WidgetBase::formSingleElement()

Generates the form element for a single copy of the widget.

3 calls to WidgetBase::formSingleElement()
FileWidget::formMultipleElements in core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php
Overrides \Drupal\Core\Field\WidgetBase::formMultipleElements().
WidgetBase::form in core/lib/Drupal/Core/Field/WidgetBase.php
Creates a form element for a field.
WidgetBase::formMultipleElements in core/lib/Drupal/Core/Field/WidgetBase.php
Special handling to create form elements for multiple values.

File

core/lib/Drupal/Core/Field/WidgetBase.php, line 450

Class

WidgetBase
Base class for 'Field widget' plugin implementations.

Namespace

Drupal\Core\Field

Code

protected function formSingleElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $element += [
        '#field_parents' => $form['#parents'],
        // Only the first widget should be required.
'#required' => $delta == 0 && $this->fieldDefinition
            ->isRequired(),
        '#delta' => $delta,
        '#weight' => $delta,
    ];
    $element = $this->formElement($items, $delta, $element, $form, $form_state);
    if ($element) {
        // Allow modules to alter the field widget form element.
        $context = [
            'form' => $form,
            'widget' => $this,
            'items' => $items,
            'delta' => $delta,
            'default' => $this->isDefaultValueWidget($form_state),
        ];
        \Drupal::moduleHandler()->alter([
            'field_widget_single_element_form',
            'field_widget_single_element_' . $this->getPluginId() . '_form',
        ], $element, $form_state, $context);
    }
    return $element;
}

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