Problem/Motivation
Using media library form display with an image that serves as a preview. When the widget for that image is set to "Image (Focal Point)" the validation errors reporting when trying to add a new image in a media library modal breaks.
Steps to reproduce
- Use media library as a widget on an image field of some content type.
- On media library form display for image media type set "Image (Focal Point)" for widget of the image field
- Have one of the fields of image media type required
- Go to the add/edit page of the related content type
- Open modal dialog (media library) for adding new image
- Instead of selecting image, add a new image (drag and drop or select an image from file browser)
- The form for adding new image media type shows up with a thumbnail as a preview, and a preview link for it
- Leave at least one required field empty
- Press save/submit
- Validation errors on required fields should be shown
- An error in the request is shown instead
TypeError: implode(): Argument #1 ($pieces) must be of type array, string given in implode() (line 28 of /app/web/core/lib/Drupal/Core/Form/FormElementHelper.php).
- A warning is shown in dblog
Warning: Undefined array key "#parents" in Drupal\Core\Form\FormElementHelper::getElementByName() (line 28 of /app/web/core/lib/Drupal/Core/Form/FormElementHelper.php)
When debugging it was found out that "thumbnail" element is missing "#parents" and "#array_parents" keys.
The #parents should be array holding [0 => "thumbnail"]
The #array_parents should hold [0 => "media" , 1 => 0, 2 => "preview", 3 => "thumbnail"]
The following piece of code was supposed to remove default image widget template when using media library:
// Override the default Image Widget template when using the Media Library
// module so we can use the image field's preview rather than the preview
// provided by Media Library.
if ($form['#form_id'] == 'media_library_upload_form' || $form['#form_id'] == 'media_library_add_form') {
$element['#theme'] = 'focal_point_media_library_image_widget';
unset($form['media'][0]['preview']);
}
It does not work because the form_id of the upload form is not "media_library_upload_form" but "media_library_add_form_upload"
Proposed resolution
Correct the form_id in condition to check for "media_library_add_form_upload" instead of "media_library_upload_form"