diff --git a/core/lib/Drupal/Core/Field/WidgetBase.php b/core/lib/Drupal/Core/Field/WidgetBase.php index 4624ab0..0e57903 100644 --- a/core/lib/Drupal/Core/Field/WidgetBase.php +++ b/core/lib/Drupal/Core/Field/WidgetBase.php @@ -71,8 +71,7 @@ public function form(FieldItemListInterface $items, array &$form, array &$form_s // If the widget is handling multiple values (e.g Options), or if we are // displaying an individual element, just get a single form element and make // it the $delta value. - $definition = $this->getPluginDefinition(); - if (isset($get_delta) || $definition['multiple_values']) { + if ($this->handlesMultipleValues() || isset($get_delta)) { $delta = isset($get_delta) ? $get_delta : 0; $element = array( '#title' => check_plain($this->fieldDefinition->getLabel()), @@ -279,20 +278,22 @@ public function extractFormValues(FieldItemListInterface $items, array $form, ar $values = NestedArray::getValue($form_state['values'], $path, $key_exists); if ($key_exists) { - // Remove the 'value' of the 'add more' button. - unset($values['add_more']); + // Account for drag-and-drop reordering if needed. + if (!$this->handlesMultipleValues()) { + // Remove the 'value' of the 'add more' button. + unset($values['add_more']); - // The original delta, before drag-and-drop reordering, is needed to - // route errors to the corect form element. - foreach ($values as $delta => &$value) { - $value['_original_delta'] = $delta; + // The original delta, before drag-and-drop reordering, is needed to + // route errors to the corect form element. + foreach ($values as $delta => &$value) { + $value['_original_delta'] = $delta; + } + + usort($values, function ($a, $b) { + return SortArray::sortByKeyInt($a, $b, '_weight'); + }); } - // Account for drag-and-drop reordering. - usort($values, function ($a, $b) { - return SortArray::sortByKeyInt($a, $b, '_weight'); - }); - // Let the widget massage the submitted values. $values = $this->massageFormValues($values, $form, $form_state); @@ -303,7 +304,7 @@ public function extractFormValues(FieldItemListInterface $items, array $form, ar // Put delta mapping in $form_state, so that flagErrors() can use it. $field_state = field_form_get_state($form['#parents'], $field_name, $form_state); foreach ($items as $delta => $item) { - $field_state['original_deltas'][$delta] = $item->_original_delta; + $field_state['original_deltas'][$delta] = isset($item->_original_delta) ? $item->_original_delta : $delta; unset($item->_original_delta, $item->_weight); } field_form_set_state($form['#parents'], $field_name, $form_state, $field_state); @@ -341,8 +342,7 @@ public function flagErrors(FieldItemListInterface $items, array $form, array &$f // Only set errors if the element is accessible. if (!isset($element['#access']) || $element['#access']) { - $definition = $this->getPluginDefinition(); - $is_multiple = $definition['multiple_values']; + $handles_multiple = $this->handlesMultipleValues(); $violations_by_delta = array(); foreach ($field_state['constraint_violations'] as $violation) { @@ -361,7 +361,7 @@ public function flagErrors(FieldItemListInterface $items, array $form, array &$f // Pass violations to the main element: // - if this is a multiple-value widget, // - or if the violations are at the ItemList level. - if ($is_multiple || $delta === NULL) { + if ($handles_multiple || $delta === NULL) { $delta_element = $element; } // Otherwise, pass errors by delta to the corresponding sub-element. @@ -435,4 +435,16 @@ protected function getFieldSetting($setting_name) { return $this->fieldDefinition->getSetting($setting_name); } + /** + * Returns whether the widget handles multiple values. + * + * @return bool + * TRUE if a single copy of formElement() can handle multiple field values, + * FALSE if multiple values require separate copies of formElement(). + */ + protected function handlesMultipleValues() { + $definition = $this->getPluginDefinition(); + return $definition['multiple_values']; + } + }