1

I have a image chooser in widget, when I click it first time, pop-up is open and I can choose image, but if I want choose another image and click on button I get a exception. enter image description here

widget.xml

<parameter name="image" xsi:type="block" required="true" visible="true" sort_order="10">
     <label translate="true">Banner image</label>
     <block class="Namespace\Widget\Block\Adminhtml\Widget\ImageChooser">
         <data>
              <item name="button" xsi:type="array">
                 <item name="open" xsi:type="string">Choose Image...</item>
              </item>
         </data>
     </block>
</parameter>

ImageChooser.php

class ImageChooser extends \Magento\Backend\Block\Template
{

protected $elementFactory;

/**
 * Image Chooser construct
 *
 * @param \Magento\Backend\Block\Template\Context $context
 * @param \Magento\Framework\Data\Form\Element\Factory $elementFactory
 * @param array $data
 */
public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Framework\Data\Form\Element\Factory $elementFactory,
    array $data = []
) {
    $this->elementFactory = $elementFactory;
    parent::__construct($context, $data);
}

/**
 * Prepare chooser element HTML
 *
 * @param \Magento\Framework\Data\Form\Element\AbstractElement $element Form Element
 * @return \Magento\Framework\Data\Form\Element\AbstractElement
 */
public function prepareElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
{
    $config = $this->_getData('config');
    $sourceUrl = $this->getUrl(
        'cms/wysiwyg_images/index',
        ['target_element_id' => $element->getId(), 'type' => 'file']
    );
    $chooser = $this->getLayout()->createBlock('Magento\Backend\Block\Widget\Button')
        ->setType('button')
        ->setClass('btn-chooser')
        ->setLabel($config['button']['open'])
        ->setOnClick('MediabrowserUtility.openDialog(\''. $sourceUrl .'\')')
        ->setDisabled($element->getReadonly());
    $input = $this->elementFactory->create("text", ['data' => $element->getData()]);
    $input->setId($element->getId());
    $input->setForm($element->getForm());
    $input->setClass("widget-option input-text admin__control-text");
    if ($element->getRequired()) {
        $input->addClass('required-entry');
    }
    $element->setData('after_element_html', $input->getElementHtml() . $chooser->toHtml());
    return $element;
}

Has anyone encountered this error?

1
  • I fixed it, just need code setOnClick( "MediabrowserUtility.openDialog( '$sourceUrl', null, null, '".$this->escapeQuote(__('Choose Image...'),true)."', (opt = new Object(), opt.closed = false, opt) )" ) code Commented Apr 12, 2017 at 7:09

1 Answer 1

4

Add media script in the element of set data:

$element->setData('after_element_html', $input->getElementHtml() . $chooser->toHtml() . "<script>require(['mage/adminhtml/browser']);</script>");
1
  • This should be the accepted answer. It works perfectly. Commented Feb 13, 2019 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.