How to program the media browser in Magento 2
Being a jQuery UI widget Magento 2 media browser provides administrators with the ability to use a media library. Thus, they can easily upload images to media library and choose images from it.
The widget is called $.mage.mediabrowser. Besides, there is a global utility object named MediabrowserUtility used for opening and closing media browser (openDialog and closeDialog methods). Both are defined in the lib/web/mage/adminhtml/browser.js AMD module loadable via the mage/adminhtml/browser name. Use the following code to open the media browser in Magento 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
define(['jquery','mage/adminhtml/browser' ], function($) {return function(config) { $('.some-button').click(function(){ /** @type Object */ var cc = config['coreConfig']; /** @type String */ var url = cc['files_browser_window_url'] + 'target_element_id/' + config.id + '/'; /** @type ?Number */ var storeId = cc['store_id']; if (storeId) { url += 'store/' + storeId; } MediabrowserUtility.openDialog(url); }); };}); |