Focal point image widget causes validation errors not to be shown

Created on 4 August 2022, over 2 years ago
Updated 2 November 2023, about 1 year ago

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"

πŸ› Bug report
Status

Needs work

Version

2.0

Component

Media Integration

Created by

πŸ‡·πŸ‡ΈSerbia petar_basic

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡¦πŸ‡ΊAustralia dpi Perth, Australia

    Thanks, this fixed an issue wherein if no Alternative text was entered after using quick add, then the form would refresh with no errors displayed.

    Patch correctly shows errors again.

  • πŸ‡ΊπŸ‡ΈUnited States Nuuou Lincoln, NE

    We ran into two issues using Focal Point. Both this issue and #3088536 πŸ› Warning - Theme hook focal_point_media_library_image_widget not found. Needs review

    Those two patches conflict though, so here's an additional patch combining the two.

  • πŸ‡ΊπŸ‡ΈUnited States Nuuou Lincoln, NE

    Upon further testing, the inclusion of patch #4 actually caused more issues with Drupal 10.1 for me.
    Specifically, received the warning:

    Warning: Undefined array key "#source_field_name" in/app/web/core/themes/claro/claro.theme on line 1487

    Adjusting this patch further to just remove the entire if-check seems to have fixed this for me. I feel like the way this widget works has evolved over time and needs more thought into it outside of just this patch.

    Still need more testing. Also setting this against 2.x-dev now.

  • Status changed to Needs work about 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States Nuuou Lincoln, NE
Production build 0.71.5 2024