diff --git a/core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php b/core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php index 5d808bb067..38f4cc5692 100644 --- a/core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php +++ b/core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php @@ -2,7 +2,6 @@ namespace Drupal\image\Plugin\Field\FieldWidget; -use Drupal\Component\Utility\NestedArray; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Image\ImageFactory; @@ -186,33 +185,6 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen return $element; } - /** - * {@inheritdoc} - */ - public function massageFormValues(array $values, array $form, FormStateInterface $form_state) { - // Since the file upload widget now supports uploads of more than one file - // at a time it always returns an array of fids. We have to translate this - // to a single fid, as the field expects a single value. In this process, we - // have to unset any image dimension for files other than the first, so that - // the dimensions are not copied from those of the first file. - $new_values = []; - foreach ($values as &$value) { - $first = TRUE; - foreach ($value['fids'] as $fid) { - $new_value = $value; - $new_value['target_id'] = $fid; - unset($new_value['fids']); - if (!$first) { - unset($new_value['width'], $new_value['height']); - } - $new_values[] = $new_value; - $first = FALSE; - } - } - - return $new_values; - } - /** * Form API callback: Processes an image_image field element. * @@ -235,9 +207,8 @@ public static function process($element, FormStateInterface $form_state, $form) ]; // Determine image dimensions. - if (isset($element['#value']['width']) && isset($element['#value']['height'])) { - $variables['width'] = $element['#value']['width']; - $variables['height'] = $element['#value']['height']; + if ($form_state->has('image_preview_dimensions')) { + $variables += $form_state->get('image_preview_dimensions'); } else { $image = \Drupal::service('image.factory')->get($file->getFileUri()); @@ -261,14 +232,7 @@ public static function process($element, FormStateInterface $form_state, $form) // Store the dimensions in the form so the file doesn't have to be // accessed again. This is important for remote files. - $element['width'] = [ - '#type' => 'hidden', - '#value' => $variables['width'], - ]; - $element['height'] = [ - '#type' => 'hidden', - '#value' => $variables['height'], - ]; + $form_state->set('image_preview_dimensions', ['width' => $variables['width'], 'height' => $variables['height']]); } elseif (!empty($element['#default_image'])) { $default_image = $element['#default_image']; @@ -313,70 +277,6 @@ public static function process($element, FormStateInterface $form_state, $form) return parent::process($element, $form_state, $form); } - /** - * Form submission handler for upload/remove button of formElement(). - * - * This runs in addition to and after file_managed_file_submit(). - * - * @see file_managed_file_submit() - */ - public static function submit($form, FormStateInterface $form_state) { - // During the form rebuild, formElement() will create field item widget - // elements using re-indexed deltas, so clear out FormState::$input to - // avoid a mismatch between old and new deltas. The rebuilt elements will - // have #default_value set appropriately for the current state of the field, - // so nothing is lost in doing this. - $button = $form_state->getTriggeringElement(); - $parents = array_slice($button['#parents'], 0, -2); - NestedArray::setValue($form_state->getUserInput(), $parents, NULL); - - // Go one level up in the form, to the widgets container. - $element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -1)); - $field_name = $element['#field_name']; - $parents = $element['#field_parents']; - - $submitted_values = NestedArray::getValue($form_state->getValues(), array_slice($button['#parents'], 0, -2)); - foreach ($submitted_values as $delta => $submitted_value) { - if (empty($submitted_value['fids'])) { - unset($submitted_values[$delta]); - } - } - - // If there are more files uploaded via the same widget, we have to - // separate them, as we display each file in its own widget. In this - // process, we have to unset any image dimension for files after the first, - // that would otherwise be copied from those of the first file. - $new_values = []; - foreach ($submitted_values as $delta => $submitted_value) { - if (is_array($submitted_value['fids'])) { - $first = TRUE; - foreach ($submitted_value['fids'] as $fid) { - $new_value = $submitted_value; - $new_value['fids'] = [$fid]; - if (!$first) { - unset($new_value['width'], $new_value['height']); - } - $new_values[] = $new_value; - $first = FALSE; - } - } - else { - $new_value = $submitted_value; - } - } - - // Re-index deltas after removing empty items. - $submitted_values = array_values($new_values); - - // Update form_state values. - NestedArray::setValue($form_state->getValues(), array_slice($button['#parents'], 0, -2), $submitted_values); - - // Update items. - $field_state = static::getWidgetState($parents, $field_name, $form_state); - $field_state['items'] = $submitted_values; - static::setWidgetState($parents, $field_name, $form_state, $field_state); - } - /** * Validate callback for alt and title field, if the user wants them required. *