Fails in Media Library modal (input name/classname mismatch)

Created on 18 October 2024, 6 months ago

Problem/Motivation

The active_tags widget fails to initialize when adding a Media item in a Media Library modal. The javascript console error is:

drupal.js?v=10.3.5:64 Uncaught SyntaxError: Failed to execute 'querySelector' on 'Document': '.media[0][fields][field_tags]' is not a valid selector.
    at initAutocomplete (active-tags.init.js?v=10.3.5:124:36)
    at Array.forEach (<anonymous>)
    at Object.attach (active-tags.init.js?v=10.3.5:1179:71)
    at drupal.js?v=10.3.5:166:24
    at Array.forEach (<anonymous>)
    at Drupal.attachBehaviors (drupal.js?v=10.3.5:162:34)
    at HTMLDivElement.<anonymous> (ajax.js?v=10.3.5:1389:18)
    at Function.each (jquery.min.js?v=3.7.1:2:3129)
    at ce.fn.init.each (jquery.min.js?v=3.7.1:2:1594)
    at Drupal.AjaxCommands.insert (ajax.js?v=10.3.5:1381:19)

Background

We use core's Media Library embed in CKEditor 5 โ†’ to embed media items in text content areas. Our media items have a 'Tags' taxonomy reference field that users can edit when creating new media items during the embed process

When creating a new Media entity to embed, Media Library opens an Add form in a modal. If the active_tags widget is used for the Tags field, the input name attribute is media[0][fields][field_tags], as opposed to the more common field_tags. That results in the invalid selector in the error above -- .media[0][fields][field_tags].

Steps to reproduce

This example assumes a basic 10.3.x install with Basic Page content type, Basic HTML text format, etc.

  1. Enable core Media and Media Library modules.
  2. Create an Image media type and add a 'Tags' taxonomy reference field.
  3. Enable the Tags field on the 'Media library' form display (/admin/structure/media/manage/image/form-display/media_library) for the Image media type, and set its widget to 'Autocomplete (Active Tags)'.
  4. Enable and configure Media Library embedding on the Basic HTML text format (/admin/config/content/formats/manage/basic_html).
  5. Create a new Basic Page content item.
  6. In the body, click the 'Insert Media' button.
  7. This opens the 'Add or select' modal
  8. Select an example image file to upload.

The modal should now display the form defined by the 'Media library' form display. The 'Tags' autocomplete will be non-functional, and the error above should be in the browser console.

Proposed resolution

Based on my testing, the autocomplete input element's data-identifier attribute consistently has the desired identifier/selector -- a simple solution might be to prefer that attribute value over name in active-tags.init.js. So, line 22:

const identifier = input.name || input.dataset.identifier;

becomes

const identifier = input.dataset.identifier || input.name;

Though I can't say definitively that works as desired in all cases.

Remaining tasks

  • Open MR
  • Review and test
  • Merge

User interface changes

None.

API changes

None.

Data model changes

None.

๐Ÿ› Bug report
Status

Active

Version

1.0

Component

Code

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States justcaldwell Austin, Texas

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

Merge Requests

Comments & Activities

  • Issue created by @justcaldwell
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States justcaldwell Austin, Texas
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States justcaldwell Austin, Texas
  • Pipeline finished with Success
    6 months ago
    Total: 225s
    #314133
  • Pipeline finished with Success
    6 months ago
    #314134
  • ๐Ÿ‡ต๐Ÿ‡ฑPoland mahyarss

    Thank you, @justcaldwell, for reporting this issue and submitting a Merge Request!

    I have reviewed and accepted the proposed changes in MR. The issue is now resolved, and the fix has been successfully merged into the branch (1.0.x).

    Marking the status as Fixed. If any additional feedback or issues arise, please feel free to share.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States justcaldwell Austin, Texas

    Great, thanks @mahyarsbt!

    I see the MR was approved, but it seems like it's not actually merged to 1.0.x?

  • ๐Ÿ‡ต๐Ÿ‡ฑPoland mahyarss

    Hi @justcaldwell,

    Thank you for pointing that out! Your observation is correctโ€”the changes from your Merge Request were not merged directly but were included as part of a broader update to the module.

    I have applied your proposed fix, along with several other improvements, and published them in the latest development version: 1.0.x-dev (updated on 6 Dec 2024 at 23:03 UTC). The fix for the input name/classname mismatch in the Media Library modal is now available in this release.

    Feel free to test the development version and let me know if everything works as expected or if thereโ€™s anything else to address. Thank you again for your valuable contribution!

    Best regards,
    Mahyar

  • ๐Ÿ‡ต๐Ÿ‡ฑPoland mahyarss
Production build 0.71.5 2024