Make WordPress Core

Opened 9 years ago

Closed 7 years ago

#23096 closed enhancement (fixed)

Provide callback before setting state of the editor media manager

Reported by: griffinjt Owned by: wonderboymusic
Milestone: 4.1 Priority: normal
Severity: normal Version: 3.5
Component: Media Keywords: has-patch
Focuses: Cc:

Description (last modified by SergeyBiryukov)

In the current setup, there is no way to modify the current state or views when the media manager is opened from the "Add Media" button. By the time you can modify anything, all the default views have been rendered and all of the events have been fired.

It would be helpful to check for the existence of a callback, and if it exists, run that before returning the workflow.

My patch does this, which you could invoke in this way:

wp.media.editor.extend = function(workflow){
        // Manipulate the workflow here

Thoughts? This is only for the default media manager that is invoked by clicking the Add Media button.

Attachments (2)

23096.diff (604 bytes) - added by griffinjt 9 years ago.
23096.2.diff (520 bytes) - added by wonderboymusic 7 years ago.

Download all attachments as: .zip

Change History (12)

9 years ago

#1 @griffinjt
9 years ago

Oops, wrong syntax for code block. This should look better:

wp.media.editor.extend = function(workflow){
        // Manipulate workflow here

#2 @SergeyBiryukov
9 years ago

  • Description modified (diff)

#3 @SergeyBiryukov
9 years ago

  • Version changed from trunk to 3.5

#4 @sc0ttkclark
9 years ago

  • Cc lol@… added

WANT / NEED. Caps on purpose.

#5 @husobj
9 years ago

  • Cc ben@… added

#6 @ericlewis
7 years ago

What is your goal here?

You can extend/override the controller media.view.MediaFrame.Whatever to do whatever you want.

I think you may be dreaming of #21170.

#7 @wonderboymusic
7 years ago

  • Keywords dev-feedback removed
  • Milestone changed from Awaiting Review to 4.1

If you want to override the click handler, you should remove it and add your own - since the current click handler is delegated, and isn't namespaced, this is messy. My patch 23096.2.diff namespaces the click handler: click.add_media_button

With it, you can just do:

$( document.body )
    .off( 'click.add_media_button' )
    .on( 'click.add_media_button', '.insert-media', function ( event ) { 
        var elem = $( event.currentTarget ),
	    editor = elem.data('editor'),
	    options = {
		frame:    '{WHATEVER_YOU_WANT}',
		state:    '{WHATEVER_YOU_WANT}',
		title:    '{WHATEVER_YOU_WANT}',


        wp.media.editor.open( editor, options );
} )

#8 @nacin
7 years ago

We should probably use hyphens for these namespaces.

I'm mostly good with this except that we're solving a bit of an interesting line of code in the existing callback:

// Remove focus from the `.insert-media` button.
// Prevents Opera from showing the outline of the button
// above the modal.
// See: http://core.trac.wordpress.org/ticket/22445

We probably shouldn't expect plugin authors to see this and copy this over. but also, might there be something else we'll want to always do in the future?

This is a good use case for firing a custom event inside the callback, seems like. Or could we suggest they attach an event and call stopImmediatePropagation? Probably not, as they'd want to set media-editor.js as a dependency and then it'd load first...

#9 @wonderboymusic
7 years ago

I think there is an overall issue with separating library code and docReady init code. Our Backbone code should be a library of classes that can be overridden by extension/inheritance (already is), and the "init" portion should be what can easily be short-circuited.

For this particular instance, I will add the namespace (with dashes!) to the click registration, which will allow devs to turn off the default behavior. Messy, but solves the immediate problem.

The docReady code problem is across the codebase, and something I hope to address soon when an elegant solution arises.

#10 @wonderboymusic
7 years ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from new to closed

In 29802:

Add a namespace to the click handler registration for .insert-media in media-editor.js: click.add-media-button.

This is a stopgap to allow devs to override the default behavior.

Fixes #23096.

Note: See TracTickets for help on using tickets.