diff --git a/core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php b/core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php index 3f216c5817..6e506d773e 100644 --- a/core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php +++ b/core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php @@ -7,6 +7,7 @@ use Drupal\Core\Image\ImageFactory; use Drupal\Component\Utility\NestedArray; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Render\Element; use Drupal\Core\Render\ElementInfoManagerInterface; use Drupal\file\Entity\File; use Drupal\file\Plugin\Field\FieldWidget\FileWidget; @@ -194,11 +195,6 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen * This method is assigned as a #process callback in formElement() method. */ public static function process($element, FormStateInterface $form_state, $form) { - if (count($element['#files']) > 1) { - // In the case of a multiple upload, skip the processing, since it will be - // processing later at the appropriate stage. - return parent::process($element, $form_state, $form); - } $item = $element['#value']; $item['fids'] = $element['fids']['#value']; @@ -236,17 +232,6 @@ public static function process($element, FormStateInterface $form_state, $form) '#style_name' => $variables['style_name'], '#uri' => $variables['uri'], ]; - - // 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'], - ]; } elseif (!empty($element['#default_image'])) { $default_image = $element['#default_image']; @@ -291,6 +276,44 @@ public static function process($element, FormStateInterface $form_state, $form) return parent::process($element, $form_state, $form); } + /** + * Form API callback: Processes a group of image_image field elements. + * + * Expands the image_image type to include the width and height. + * + * This method is assigned as a #process callback in ::formMultipleElements(). + */ + public static function processMultiple($element, FormStateInterface $form_state, $form) { + $element = parent::processMultiple($element, $form_state, $form); + $element_children = Element::children($element, TRUE); + + foreach ($element_children as $child) { + if (!empty($child['#files'])) { + $file = reset($child['#files']); + $image = \Drupal::service('image.factory')->get($file->getFileUri()); + if ($image->isValid()) { + $width = $image->getWidth(); + $height = $image->getHeight(); + } + else { + $width = $height = NULL; + } + // Store the dimensions in the form so the file doesn't have to be + // accessed again. This is important for remote files. + $element[$child]['width'] = [ + '#type' => 'hidden', + '#value' => $width, + ]; + $element[$child]['height'] = [ + '#type' => 'hidden', + '#value' => $height, + ]; + } + } + + return $element; + } + /** * Validate callback for alt and title field, if the user wants them required. * diff --git a/core/modules/image/tests/src/FunctionalJavascript/ImageFieldWidgetMultipleTest.php b/core/modules/image/tests/src/FunctionalJavascript/ImageFieldWidgetMultipleTest.php index 9c374aefe6..84c1a1a05b 100644 --- a/core/modules/image/tests/src/FunctionalJavascript/ImageFieldWidgetMultipleTest.php +++ b/core/modules/image/tests/src/FunctionalJavascript/ImageFieldWidgetMultipleTest.php @@ -4,7 +4,7 @@ use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\FunctionalJavascriptTests\DrupalSelenium2Driver; -use Drupal\FunctionalJavascriptTests\JavascriptTestBase; +use Drupal\FunctionalJavascriptTests\WebDriverTestBase; use Drupal\node\Entity\Node; use Drupal\Tests\image\Kernel\ImageFieldCreationTrait; use Drupal\Tests\TestFileCreationTrait; @@ -14,7 +14,7 @@ * * @group image */ -class ImageFieldWidgetMultipleTest extends JavascriptTestBase { +class ImageFieldWidgetMultipleTest extends WebDriverTestBase { use ImageFieldCreationTrait; use TestFileCreationTrait;