• Resolved michaelw90

    (@michaelw90)


    I’ve created an image upload field with cmb2. I’m trying to develop click functionality for the returned image, but when I click on the image the file upload modal pops up. I would like this to only open when clicking the “Upload File” button. This way I can work with the returned image.

    How can I change this behaviour?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I’m curious what events are tied to the spot in question, and how you could possibly override it with your own custom JS. Perhaps a return early or event.preventDefault() if a value is already se.

    Thread Starter michaelw90

    (@michaelw90)

    I’ve tried event.preventDefault(), but it doesn’t seem to make any difference.

    I’m looking at the cmb2.js, could there be anything here that can change the opening of the modal?

    handlers.openModal = function() {
    	var selection = modal.state().get( 'selection' );
    	var attach;
    
    	if ( ! cmb.attach_id ) {
    		selection.reset();
    	} else {
    		attach = wp.media.attachment( cmb.attach_id );
    		attach.fetch();
    		selection.set( attach ? [ attach ] : [] );
    	}
    
    	cmb.trigger( 'cmb_media_modal_open', selection, media );
    };
    
    // When a file is selected, run a callback.
    modal
    	.on( 'select', handlers.selectFile )
    	.on( 'open', handlers.openModal );
    
    // Finally, open the modal
    modal.open();
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I’m hoping Justin, the lead dev on CMB2, can jump in sometime soon, as he knows the plugin’s javascript, as well as javascript in general, a lot more than I do.

    Thread Starter michaelw90

    (@michaelw90)

    I managed to stop the modal opening by adding stopImmediatePropagation() to the event! ??

    • This reply was modified 7 years, 9 months ago by michaelw90.
    Plugin Author Justin Sternberg

    (@jtsternberg)

    My only caution would be to make sure it doesn’t break other file/file_list fields. You can test the theory by registering a new test box/field and making sure it still works normally. There are many plugins/themes that rely on CMB2 working properly.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Secondary idea to what Justin said, add some unique selectors around this field from your config for this, so that you can add specificity to the js selector that’s using .stopImmediatePropagation()

    Thread Starter michaelw90

    (@michaelw90)

    Good idea, I’ll make sure to change the selectors.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Is there a way to prevent File Modal from opening when clicking on image?’ is closed to new replies.