Insert Media sub-module missing .insert-template class on added elements

Created on 27 March 2024, 3 months ago
Updated 16 April 2024, 2 months ago

Problem/Motivation

When using Insert + Insert Media on a textarea clicking the "Insert" button fails to insert the media embed tag and instead causes the page to reload.

Proposed resolution

The issue is that at some point the code changes so that insert templates now need to to have the .insert-template class added to the element. But the code in insert_media wasn't updated.

Adding '#attributes' => ['class' => ['insert-template']], to the template elements added in insert_media_insert_process() should fix the issue.

πŸ› Bug report
Status

Fixed

Version

3.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States eojthebrave Minneapolis, MN

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

Merge Requests

Comments & Activities

  • Issue created by @eojthebrave
  • Status changed to Needs review 3 months ago
  • πŸ‡ΊπŸ‡ΈUnited States eojthebrave Minneapolis, MN

    I was able to get media_insert working again after making a few changes.

    I refactored the code in insert_media.module to make use of hook_insert_styles, hook_insert_render, and hook_insert_variables instead of relying on hook_insert_process to do all the the work. Amongst other things this solves an issue where the insert-template elements that the JS code is looking for where not getting added to the page with the correct class.

    I modified Drupal.insert.Inserter.#insert so that it passes the `event` when called and then if successful calls `event.preventDefault()`. Without this, when used with a media library widget, the 'click' on the Insert button would propagate to the "Remove" button for the media entity card. So after the embed code is inserted it would then remove the entity from the media field, and cause the form to reload.

    In order to handle the fact that the media library widgets can have multiple values, and each one requires a different insert-template, I added the `$insertElement` as an argument to hook_insert_render(). This allows access to the media entity associated with each Insert button.

    Without these changes when you try and use media_insert it:

    • Won't insert the embed code because the Handler.js code can't find the template element
    • The page is refreshed every time you click the Insert button
  • Status changed to Fixed 3 months ago
  • Thanks a lot for the patch. I was able to reproduce the issue. The fix is part of 3.0.0-beta6 β†’ .

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.69.0 2024