diff --git a/core/lib/Drupal/Core/Field/WidgetBase.php b/core/lib/Drupal/Core/Field/WidgetBase.php index 8fa66fd39a..87451b69ef 100644 --- a/core/lib/Drupal/Core/Field/WidgetBase.php +++ b/core/lib/Drupal/Core/Field/WidgetBase.php @@ -248,16 +248,16 @@ protected function formMultipleElements(FieldItemListInterface $items, array &$f 'form' => $form, 'widget' => $this, 'items' => $items, - 'delta' => $delta, 'default' => $this->isDefaultValueWidget($form_state), + 'cardinality' => $cardinality, ]; \Drupal::moduleHandler()->alter([ 'field_widget_multiple_form', 'field_widget_multiple_' . $this->getPluginId() . '_form', ], $elements, $form_state, $context); } - } + return $elements; } @@ -326,12 +326,14 @@ public static function addMoreAjax(array $form, FormStateInterface $form_state) * Generates the form element for a single copy of the widget. */ protected function formSingleElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { + $cardinality = $this->fieldDefinition->->getFieldStorageDefinition()->getCardinality(); $element += [ '#field_parents' => $form['#parents'], // Only the first widget should be required. '#required' => $delta == 0 && $this->fieldDefinition->isRequired(), '#delta' => $delta, '#weight' => $delta, + '#cardinality' => $cardinality, ]; $element = $this->formElement($items, $delta, $element, $form, $form_state); @@ -344,6 +346,7 @@ protected function formSingleElement(FieldItemListInterface $items, $delta, arra 'items' => $items, 'delta' => $delta, 'default' => $this->isDefaultValueWidget($form_state), + 'cardinality' => $cardinality, ]; \Drupal::moduleHandler()->alter(['field_widget_form', 'field_widget_' . $this->getPluginId() . '_form'], $element, $form_state, $context); } diff --git a/core/modules/field/field.api.php b/core/modules/field/field.api.php index 1fd8427e8c..d9708db87c 100644 --- a/core/modules/field/field.api.php +++ b/core/modules/field/field.api.php @@ -179,6 +179,8 @@ function hook_field_widget_info_alter(array &$info) { * - delta: The order of this item in the array of subelements (0, 1, 2, etc). * - default: A boolean indicating whether the form is being shown as a dummy * form to set default values. + * - cardinality: The cardinality of the field in the field storage. See + * \Drupal\Core\Field\FieldStorageDefinitionInterface for possible values. * * @see \Drupal\Core\Field\WidgetBaseInterface::form() * @see \Drupal\Core\Field\WidgetBase::formSingleElement() @@ -240,12 +242,13 @@ function hook_field_widget_WIDGET_TYPE_form_alter(&$element, \Drupal\Core\Form\F * - widget: The widget plugin instance. * - items: The field values, as a * \Drupal\Core\Field\FieldItemListInterface object. - * - delta: The order of this item in the array of subelements (0, 1, 2, etc). * - default: A boolean indicating whether the form is being shown as a dummy * form to set default values. + * - cardinality: The cardinality of the field in the field storage. See + * \Drupal\Core\Field\FieldStorageDefinitionInterface for possible values. * * @see \Drupal\Core\Field\WidgetBaseInterface::form() - * @see \Drupal\Core\Field\WidgetBase::formSingleElement() + * @see \Drupal\Core\Field\WidgetBase::formMultipleElements() * @see hook_field_widget_form_alter() * @see hook_field_widget_multiple_WIDGET_TYPE_form_alter() */ @@ -274,7 +277,7 @@ function hook_field_widget_multiple_form_alter(array &$elements, \Drupal\Core\Fo * and content of the array. * * @see \Drupal\Core\Field\WidgetBaseInterface::form() - * @see \Drupal\Core\Field\WidgetBase::formSingleElement() + * @see \Drupal\Core\Field\WidgetBase::formMultipleElements() * @see hook_field_widget_multiple_form_alter() */ function hook_field_widget_multiple_WIDGET_TYPE_form_alter(array &$elements, \Drupal\Core\Form\FormStateInterface $form_state, array $context) {