You are here

protected function WidgetBase::formSingleElement in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Field/WidgetBase.php \Drupal\Core\Field\WidgetBase::formSingleElement()
  2. 10 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 344

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()
      ->alterDeprecated('Deprecated in drupal:9.2.0 and is removed from drupal:10.0.0. Use hook_field_widget_single_element_form_alter or hook_field_widget_single_element_WIDGET_TYPE_form_alter instead. See https://www.drupal.org/node/3180429.', [
      'field_widget_form',
      'field_widget_' . $this
        ->getPluginId() . '_form',
    ], $element, $form_state, $context);
    \Drupal::moduleHandler()
      ->alter([
      'field_widget_single_element_form',
      'field_widget_single_element_' . $this
        ->getPluginId() . '_form',
    ], $element, $form_state, $context);
  }
  return $element;
}