I have doubts in Magento2 widget. In my custom widget I have added the CMS block through chooser then saved successfully. When I come to edit, I need to remove the selected cms block in the widget.

How can I remove the selected cms block?

enter image description here


By default magento2 have no remove button, so you need to do some work around for this.

You can create new class which is extended by CMS block chooser class (Magento\Cms\Block\Adminhtml\Block\Widget\Chooser).

namespace Sathish\Cms\Block\Adminhtml\Block\Widget;

class Chooser extends Magento\Cms\Block\Adminhtml\Block\Widget\Chooser

     * 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)
    $uniqId = $this->mathRandom->getUniqueHash($element->getId());

    $html = $chooser->toHtml();
        $html .= '<span class="action-default scalable btn-chooser" onClick="jQuery(\'#'.$uniqId.'label\').text(\'Not Selected\');jQuery(\'#'.$uniqId.'value\').val(\'\');">Remove</span>';
        $element->setData('after_element_html', $html);
        return $element;

here you can able to get the unique id of block.

  • for selected block label magento used this format for Id: unique id + 'label'
  • for selected block value magento used this format for Id: unique id + 'value'

then append some button including js snippets in existing prepared HTML like above said.

I hope this will help you to achieve your goal. thanks.

  • It works for me.
    – Renga
    Jun 1 '18 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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