Problem/Motivation

When using other modules, such as media entity file replace, which add their own custom submit handler, this module prevents those from working. It does this by completely clearing out the value of $form['actions']['submit']['#submit'] and then applying an ajax submit handler instead.

Steps to reproduce

  1. Install the media entity file replace module and add it to the form for at least one media type
  2. Enable this module for any given media entity reference field
  3. Edit an existing media entity using this module that is also using media entity file replace, and try uploading a replacement file
  4. Save the media entity, then go to Content -> Files. You will see the file uploaded to replace the existing one, but still marked as temporary and used in 0 places
  5. Go back to editing the media entity for which you tried to replace the file, and check the file that is attached - open that file to view it, refresh a few times to make sure you are not viewing a cached image. It will be the same image as before, and not replaced as expected

Proposed resolution

See this core issue. It is aimed to resolve the same problem, and it may end up coming to Drupal core once they have worked through the various concerns identified with it.

The patch provided on that issue works, so if this module is not working for you then use that patch instead.

This module could be updated based on that latest patch in order to allow other submit handlers, such as the one in media entity file replace, to work properly.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

teknocat created an issue. See original summary.

ahebrank’s picture

teknocat’s picture

It seems to be a bit different. The version I am filing this issue against already has the fix from that other issue - it's no longer completely unsetting the #submit array. However, when this module runs its hook after any other modules that may have set other submit handlers, it strips out those handlers that still need to run because it sets the submit handlers to an empty array, without accounting for any that may be set already that need to run. I found this happens when using it in conjunction with Media Entity File Replace, for example. It strips out the submit handler set by that module.

The core patch I linked in the issue description above provides an edit button without causing any issues with submit handlers.

ahebrank’s picture

Version: 8.x-2.0 » 8.x-2.x-dev
Status: Active » Postponed

Unfortunately I think this is a wont-fix given that we're getting into a pretty complex interaction between contrib modules that are both working against core to some degree. The 1.x version of this module is much simpler and doesn't have many of these alter-related problems, or there's the core patch. Both of these are discussed on https://www.drupal.org/project/media_library_edit

I'm going to leave open for visibility but mark postponed.

prudloff made their first commit to this issue’s fork.

prudloff’s picture

We encountered a similar problem using media_library_edit with media_name.
media_name adds a custom #submit callback (after the media_library_edit_form_alter() the array, so it is kept).
But since the ::submitForm callback has been removed, the entity is not filled correctly with the new values when reaching the _media_name_form_submit callback (and it comes before _media_library_edit_media_edit_save() calls ::submitForm).

What is the reason to remove the #submit callbacks?
I tried to keep them (see MR) and everything seems to work correctly. (The #submit callbacks are executed correctly in order and then the #ajax callback closes the modal.)

ahebrank’s picture

Title: Prevents other submit handlers from working, such as media entity file replace » Don't unset the #submit callback
Status: Postponed » Needs review
prudloff’s picture

teknocat’s picture

Yes I agree there is no reason for this module to wipe out all the submit handlers just to be able to do it's thing. @prudloff's solution looks great. I don't think I would have thought of that.

ahebrank’s picture

@prudloff can you change the name of _media_library_edit_media_edit_save() since it no longer saves?

Looking for an RTBC and then can merge.

ahebrank’s picture

Version: 8.x-2.x-dev » 3.0.x-dev
Status: Needs review » Needs work
dhruv.mittal’s picture

Assigned: Unassigned » dhruv.mittal
dhruv.mittal’s picture

Assigned: dhruv.mittal » Unassigned
Status: Needs work » Needs review
divyansh.gupta’s picture

Status: Needs review » Needs work

Since there is a unresolved thread moving it to Needs Work, Please resolve the thread.

dhruv.mittal’s picture

Status: Needs work » Needs review

Please review

divyansh.gupta’s picture

Status: Needs review » Reviewed & tested by the community

I have reviewed and tested the latest patch in !11.

✅ The #submit callback is no longer being unset, allowing other submit handlers (such as from Media Entity File Replace) to function correctly.
✅ The function name update issue has been resolved, and $form['actions']['submit']['#ajax']['callback'] now correctly references the updated function.
✅ The form submission process works as expected, and the modal closes properly after submission.

Everything looks good, and I don't see any regressions or issues. Moving this to RTBC.

hargurpreet’s picture

Issue summary: View changes
StatusFileSize
new1.64 KB

I also encountered an issue while replacing a file in the modal, but this patch resolved it.

  • ahebrank committed 98479951 on 8.x-2.x authored by prudloff
    Issue #3246687 by dhruv.mittal, prudloff: Don't unset the #submit...
ahebrank’s picture

Status: Reviewed & tested by the community » Fixed
ahebrank’s picture

Status: Fixed » Needs work

That MR was targeted at 8.x-2.x so that's where it landed. So we'll need another one for 3.x, if it's intended to apply there.

hargurpreet’s picture

Issue summary: View changes

Restored the issue detail

marcoliver made their first commit to this issue’s fork.

marcoliver changed the visibility of the branch 3.0.x to hidden.

marcoliver’s picture

Status: Needs work » Needs review

Hey everybody, I ported the changes from 8.x-2.x to the 3.0.x branch and opened a new MR.

mlncn’s picture

Priority: Normal » Major
Status: Needs review » Reviewed & tested by the community

marcoliver's merge requent !26 works beautifully for 3.0.x; good time to get it in now before further development on that branch!

  • ahebrank committed 3f944f08 on 3.0.x authored by marcoliver
    Issue #3246687 by prudloff, dhruv.mittal, marcoliver: Don't unset the #...
mlncn’s picture

(Oh and respectfully upgraded to major because the way it manifests with Media Entity File Replace #3377463: Incompatibility with media_library_edit results in the file not being replaced and it is extremely difficult to figure out exactly what is happening… if you even notice that people are being served the old file because the new one was never even uploaded (silently failing, no error or warning, seeming successful)!

ahebrank’s picture

Status: Reviewed & tested by the community » Fixed
mlncn’s picture

That has to be a record in speed of commit. Thank you so much! (And everybody who worked on this fix!)

Status: Fixed » Closed (fixed)

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

usmanjutt84’s picture

The PR is merged but composer require 'drupal/media_library_edit:^3' doesn't bring the changes. Looks like module requires a new release.