Media Library validation bug when content type has a media field and a paragraph field with the same machine name

Created on 11 January 2022, almost 3 years ago
Updated 2 January 2024, 12 months ago

Problem/Motivation

I found a bug where validation of media fields fails if the content type contains a node media field with the same machine name as a paragraph media field. When Media Library attempts to validate the media item added via Layout Paragraphs it finds the wrong form triggering element because the media library widget's submit buttons of both media fields have the same name.
A related problem exists when editing a paragraph and deleting the media item. The wrong media is deleted.

Steps to reproduce

  • Create a paragraph type called Image with a Media reference field with machine name field_media. Use the Media Library widget on the form display.
  • Create a content type and add a Media reference field with machine name field_media. Make it a required field and limit allowed number of values to 1.
  • Add a Paragraph reference field that uses the Image paragraph type created in step 1. Configure to use the Layout Paragraphs widget
  • Add a new node and upload an image to the media field.
  • Add a layout section to the LP paragraph field and try to add an Image paragraph. This will fail validation and you will see the error Only one item can be selected on the node media field.

Proposed resolution

Set the #parents attribute of the component form to the paragraph field name. The parent attribute will be appended to the submit button's name attribute, making it unique.

πŸ› Bug report
Status

Closed: works as designed

Version

2.0

Component

Code

Created by

πŸ‡ͺπŸ‡ΈSpain Peacog

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.

  • πŸ‡ΊπŸ‡ΈUnited States greenskin

    The patch only fixes the issue when the paragraph is contained within a region, not when the paragraph is the root element for the entity reference field. Confirmed patch needs work.

  • πŸ‡ΊπŸ‡ΈUnited States asherry

    I did a lot of debugging on this. I don't know if I would consider it regression, in that ComponentFormBase is completely new and not used in version 1. The issue is triggered when two entity forms, (not sub entity forms), have the same field name, but I believe that the fix for this is in the media library module itself.

    I wrote up a ticket in core about this. Although this is technically a contrib issue, I don't see any way of being able to fix it in a contrib context. This is going to be a problem, (and I tested it out), with other contrib modules implementing similar entity form design.

    https://www.drupal.org/project/drupal/issues/3345064#comment-14945590 πŸ› Media library field_widget_id is not unique enough Needs work

  • πŸ‡ΊπŸ‡ΈUnited States asherry

    In my opinion this ticket should be moved to "closed (works as designed)".

  • Status changed to Postponed over 1 year ago
  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

    I think we should postpone it on πŸ› Media library field_widget_id is not unique enough Needs work . Perhaps it makes sense to add tests here, to ensure it can't happen again?

  • πŸ‡ΊπŸ‡ΈUnited States azinck

    I don't believe this issue is limited to media fields. I'm seeing the same behavior with paragraph fields. I have the following structure:

    Content type with these fields:

    • field_paragraphs - entity reference revisions field using the Paragraphs (stable) widget
    • field_wide_content - entity reference revisions field using the Layout Paragraphs widget

    Then I have a paragraph type called a "Summary Box" with the following field:

    • field_paragraphs - entity reference revisions field using the Paragraphs (stable) widget

    If I add a Summary box paragraph to field_wide_content, and then try to add a paragraph into that paragraph's field_paragraphs field I don't see the new paragraph added there in the modal. But if I close the modal I can see it got added to the field_paragraphs field on the parent node instead. If I then re-open the modal to edit my Summary box paragraph I see the paragraph I added earlier (yes, the same paragraph is now in 2 places). Furthermore, edits to content in the node's field_paragraphs field are reflected in the Summary box paragraph's field_paragraphs field. This is on Layout Paragraphs 2.0.2 and Drupal 9.5.7.

  • πŸ‡ΊπŸ‡ΈUnited States azinck

    I'm very happy to say that the bug I noted in #12 seems to be resolved by the MR on this issue!

  • Status changed to Needs work over 1 year ago
  • πŸ‡¦πŸ‡ΉAustria hudri Austria

    Given the observation in #12 this does not seem to be a media library (or media library only) issue, so changing back to needs work.

  • πŸ‡¦πŸ‡ΉAustria hudri Austria
  • πŸ‡ΊπŸ‡ΈUnited States asherry

    @azinck, thanks for the example. It looks like this is in fact not only a media library issue, but it is an issue with widgets that aren't using unique enough IDs to replace content on ajax calls.

    • @azinck, in your example the actual widget that's doing the ajax replace is the paragraphs stable widget
    • I see there is an issue here πŸ› Paragraphs and Entity Browser: Weird behavior while editing a referenced entity in Entity Browser modal Needs review , and in the issue they mention that "getUniqueId" should work, but I don't believe that's true. There is likely an issue with the paragraps widgets when there are multiple instances
    • These other widget issues are popping up because layout paragraphs, (and other modules), are modules that embed multiple forms on the page, and the ajax calls were never equipped to distinguish.
    • The MR for this issue might work in some cases, but it's not the solution. The paragraphs component form is a base form, it can't use #parents because it doesn't have any, and will break other instances.
    • The issue really centers around widgets that are relying on field names, and layout paragraphs isn't. All the ajax functionality is using uuids.
  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

    As of #16 setting priority to major. At least it sounds it can be solved? And reproduced in a test?

    Do we think it should be a core job to provide and ensure the unique IDs or should each module do? (At least a core issue might need a long time to be finished, I guess - so it might make sense to create a core issue AND solve it here?)

  • πŸ‡ΊπŸ‡ΈUnited States asherry

    This isn't major, it only happens in very specific cases. It also can't be fixed here. The current patch, as already mentioned in #8, only works for instances that are in regions, and it is still breaking tests. The root of the issue is in the ajax calls made by the widgets that are actually replacing the HTML.

    I already linked to a fix that works for media library. I understand that there are issues with other widgets, like media browser, and the paragraphs stable widget. I'd be great if people can submit a patch to those modules and link them here.

  • Status changed to Closed: works as designed over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States justin2pin

    Setting this to "Closed (works as designed)" per comments #18, #16, and #10. Thanks all.

  • πŸ‡¦πŸ‡ΉAustria hudri Austria

    This patch here does not work well with paragraph behavior subforms. Better use the linked drupal/core patch #3345064 πŸ› Media library field_widget_id is not unique enough Needs work which fixes the media library issue, and nested behavior plugins (inside LP component forms) keep working.

  • πŸ‡·πŸ‡ΈSerbia super_romeo Belgrade
Production build 0.71.5 2024