Media library field_widget_id is not unique enough

Created on 28 February 2023, over 1 year ago
Updated 9 April 2024, 3 months ago

Problem/Motivation

This is very niche, but a showstopper for us. If there happens to be two different entity forms, (not subforms) with the same field name (the field that is using the media library widget), then the field_widget_id that is used to attach the selected media entities will be identical.

In MediaLibraryFieldWidgetOpener.php

    $response
      ->addCommand(new InvokeCommand("[data-media-library-widget-value=\"$widget_id\"]", 'val', [$ids]))
      ->addCommand(new InvokeCommand("[data-media-library-widget-update=\"$widget_id\"]", 'trigger', ['mousedown']));

This will simply be, for example, "field_image".

I understand that this is not technically a core issue as the only way to currently have these embedded forms is with some sort of contrib module. I do believe it's worth talking about, for a few reasons:

  • There is no way to fix this in a contrib module, they cannot alter the id that's used here
  • There are a number of feature requests that will eventually have this functionality in core, in a number of different ways. I imagine it will come up at some point. ๐Ÿ“Œ Ensure multiple entity forms can be embedded into one form Active
  • Even if this should be addressed in contrib, it would be nice to have a more centralized reference.

Steps to reproduce

The simplest way to re-create is with the contact_block module, (very high usage). Install Drupal demo_umami (the easiest way) with contact_block enabled.
Add a media reference field to the "Website feedback" contact form. "field_image", using the media library widget.
Add an instance of the website feedback contact block to the content area.
Add another instance of the website feedback contact block to the content area. (two separate blocks)
The forms themselves have proper unique IDs contact-message-feedback-form--2, contact-message-feedback-form--1, etc.
Adding media to the second block, however, will always add an image to the first block.

Proposed resolution

One potential solution is the way the id is formed. In MediaLibraryWidget::formElement the $id_suffix could include more criteria. I attached a patch with the entity uuid, but this isn't meant to be a real solution, this is just to fix it for now and use in composer.

Another potential, (and maybe better) solution is the InvokeCommand used in MediaLibraryFieldWidgetOpener. Perhaps this selector should have a wrapper element ID.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

๐Ÿ› Bug report
Status

Needs work

Version

11.0 ๐Ÿ”ฅ

Component
Mediaย  โ†’

Last updated about 8 hours ago

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States asherry

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

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

Production build 0.69.0 2024