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?
1 Answer 1
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.