When using a Media widget on a form, where the part of the form that includes the Media widget gets AJAX reloaded, will break the widget.

This can happen when using Fieldable Panels Panes:

  1. Create an FPP type with a file using the Media widget - mark that field as required
  2. Using the Panels IPE, customize a Panel and select the FPP type from above
  3. Without attaching a file to the required field, click "Save"
  4. The form will reload in the CTools modal, telling you that the field is required, but the Media widget will show a text field (in addition to the "Browse" button) and be non-functional

Digging into this, the problem is basically that the Javascript can't find the Media widget because it has a different ID than what the Javascript is expecting. I'll post a patch soon.

Comments

dsnopek created an issue. See original summary.

dsnopek’s picture

Status: Active » Needs review
StatusFileSize
new1.13 KB

Here's a patch. I keep wondering if there is a way to make it less convoluted... But it works!

Status: Needs review » Needs work

The last submitted patch, 2: media-widget-ajax-2842446-2.patch, failed testing.

dsnopek’s picture

Status: Needs work » Needs review

Weird. Let's try that again

Status: Needs review » Needs work

The last submitted patch, 2: media-widget-ajax-2842446-2.patch, failed testing.

dsnopek’s picture

Status: Needs work » Needs review

Hrm. Well, the patch applies fine locally. I'm not sure what's going on with the testbot that it's different.

dsnopek’s picture

Here's a patch that combines this patch with the one on #2534724: Browser window opens twice for field image because they conflict. We need this for Panopoly. However, I'm going to mark this patch as hidden, since the one on #2 is the one that should actually get review (until #2534724 is merged at least, then I can reroll this patch to be against that).

The last submitted patch, 2: media-widget-ajax-2842446-2.patch, failed testing.

The last submitted patch, 2: media-widget-ajax-2842446-2.patch, failed testing.

joseph.olstad’s picture

Hi @dsnopek, thanks for your feedback.

I noticed your reference to #2534724: Browser window opens twice for field image
so I kicked the test bot on patch #105 , it no longer passes testing.

However I haven't noticed the described issue, are you sure that patch is still needed on an unpatched rc3?

joseph.olstad’s picture

for #2534724: Browser window opens twice for field image
#original_id was comming up undefined , see the job log for the other issue.

as for your this issue patch #2 , fails on assert 'Media file field widget adds element-specific settings.'

not sure what to make of this.

joseph.olstad’s picture

joseph.olstad’s picture

Status: Needs review » Needs work

patch doesn't pass tests, not sure yet if this is a problem with the tests or the patch.

dsnopek’s picture

@joseph.olstad: Yes, I think that patch would fix the problem. However, I'm not sure it's the right solution!

That patch is changing the CSS selector in JavaScript to use a wildcard in order to catch the 'id' with any numbers appended to it (like '--1', '--2', etc), which could catch more elements than intended. Imagine a content type with a media field on it, and then an FPP with a media field of the same name loaded in a CTools modal -- the Javascript that's intending to reload the media field in the FPP would also reload the media field on the page below because the wildcard would catch both.

While my patch is weird and confusing and I wish it were simpler, it does target exactly the elements it's intending to target (ie. only those that are reloaded via AJAX)!

dsnopek’s picture

Status: Needs work » Needs review
StatusFileSize
new1.15 KB

My patch doesn't work right for fields with multiple values. I tried for quite a while to fix it for that situation but couldn't get it working in both situations. :-(

However, the less precise fix from #2841331-3: Media browser opens under FPP overlay, inoperative does work, so I'm just gonna roll with that. Even if it's imprecise, the cases where it'd get things wrong are probably super uncommon, where as the current bug is very common.

Since #2841331: Media browser opens under FPP overlay, inoperative is closed, I'm going to re-attach the patch here, but it's written by ankurj NOT me, so that's who should get the commit credit if this is committed.

sonfd’s picture

+1 for #15

saniyat’s picture

#15 working fine with field collection.

steven jones’s picture

On our project we're doing some interesting handling of the ajax page state, and specifically how IDs are guaranteed to be unique.
What that means for us is that the way that the ID is built up and used in media module isn't stable at all.
However, I'd argue that because media is essentially using drupal_html_id to generate an ID (it's just the attribute on a form element) the ID isn't stable for media module with a plain Drupal core either.

I'd suggest just changing to use a class based upon the element ID. This should ensure 'enough' uniqueness, but because it's a class Drupal won't try and rename randomly later on in the process.

Patch attached that does just that.
The JavaScript was just using the selector straight from the PHP side, so no changes were needed there.

Status: Needs review » Needs work

The last submitted patch, 18: media-2842446-uses-classes-instead-of-ids.patch, failed testing.

steven jones’s picture

Status: Needs work » Needs review
StatusFileSize
new1.62 KB

Try and fix that test.

joseph.olstad’s picture

Status: Needs review » Fixed

committed latest patch by dsnopek to 7.x-2.x dev branch,
Thanks

Status: Fixed » Closed (fixed)

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