1

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

Sathish
8292 gold badges7 silver badges25 bronze badges
asked May 31, 2018 at 14:31

1 Answer 1

2

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.

Murtuza Zabuawala
14.8k10 gold badges47 silver badges76 bronze badges
answered May 31, 2018 at 15:54
1
  • It works for me. Commented Jun 1, 2018 at 5:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.