In #1034838: media_include_browser_js() should use #attached instead of drupal_add_* I've been working on making media module use #attached to include it's javascript and libraries, this works fine in most places except for the wysiwyg plugin it provides, where more than one javascript file is needed, so it relies on calling drupal_add_js() and drupal_add_library() from within the hook.

Would be useful to:

- allow the js definitions to take an array.
- allow a 'library' key in the plugin definition.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

sun’s picture

Category: task » feature
TwoD’s picture

Hmm, if we decide to cache the plugin metadata, adding scripts like this will fail since the hook implementation might not get called all the time and Media would be in serious trouble. (And some other plugins doing conditional actions as well.)

Most library integration plugins I've seen only require one "integration file" since they load any libraries they need regardless of whether Wyswyg is used or not (as in, Wysiwyg isn't a dependency but an optional addon) and that file only takes care of communications between the library and the editors.

This case seems a bit more complex and I'm not that familiar with Media, would you mind explaining what kind of files are loaded and why? Are all of them always needed when the Wysiwyg plugin is active (ie, would a static list of files be fine)? Are the libraries loaded used by Media even when the plugin isn't used? If so, are they loaded differently when the plugin is used?

catch’s picture

To be honest I don't know a lot about what the files are actually doing, I think it might be a file browser thing. However afaik a static list is completely fine, and at least some of the files can be loaded when the plugin isn't used. I was trying to shift everything to use #attached via a helper function, and iirc the wysiwyg hook was the only place that wasn't convertible.

giorgio79’s picture

TwoD’s picture

Status: Needs review » Active
FileSize
2.33 KB
3.54 KB

How about a patch adding the same functionality as the FAPI #attached property?
Also attaching a patch to show what Media module would need to change to take advantage of this.

UPDATE: Devin Carlson pointed out a typo in #1795968-1: Prepare for wysiwyg_load_includes being cached. That issue has a re-rolled patch for Media module.

TwoD’s picture

Status: Active » Needs review

Forgot to mention this should also work with the hook cache added in #841794: Cache wysiwyg_load_includes().

TwoD’s picture

Status: Active » Needs review
FileSize
5.96 KB

While putting together a more backwards compatible patch for Media's #1795968: Prepare for wysiwyg_load_includes being cached, I noticed they are also adding settings from the plugin hook. The #5 patch allows for this, but to keep backwards compatible with earlier versions of Wysiwyg (without caching the hook results and the #5 patch) they must also add the scripts and settings the way they do it now, using drupal_add_js().
For regular files this is fine since duplicates are ignored, but the "blacklist" array they have gets all values added twice because PHP's array_merge_recursive() will append values using numerical keys.

To avoid this, I extended the #5 patch with a check to make sure settings added via the 'attached files' property haven't already been added using drupal_add_js(). This check is currently performed every time the plugin settings are added to the page. Once the plugin meta data has been cached, the check will be very fast since the "manual" drupal_add_js() calls inside the hook implementations won't be executed, and the recursive comparison function loops over the settings array from the plugin rather than the much larger array holding all the settings added so far. Earlier versions of Wysiwyg will of course not perform the recursive comparison at all, but they'll also ignore the 'attached files' list so there's no risk of settings collision when using a newer Media module with an older Wysiwyg module.

Once there have been a couple of official releases, we can remove the recursive comparison and Media module (and others) can remove the manual drupal_add_js() calls and just use 'attached files'.

SocialNicheGuru’s picture

Issue summary: View changes

This needs to be rerolled for the newest wysiwyg module (Feb 2017)

TwoD’s picture

Status: Needs review » Needs work