Created
December 10, 2018 14:58
-
-
Save molovo/051e3882b802fa383a9b1b1b7e4f81c0 to your computer and use it in GitHub Desktop.
Wysiwyg block for Magento 2 widgets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" ?> | |
<widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Widget:etc/widget.xsd"> | |
<widget class="<vendor>\<namespace>\Block\Widget" id="my_widget"> | |
<label translate="true">Widget Name</label> | |
<description>A description</description> | |
<parameters> | |
<parameter name="text" xsi:type="block" visible="true"> | |
<label translate="true">Text</label> | |
<block class="<vendor>\<module>\Block\Adminhtml\Widget\Wysiwyg" /> | |
</parameter> | |
</parameters> | |
</widget> | |
</widgets> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace <vendor>\<module>\Block\Adminhtml\Widget; | |
use Magento\Backend\Block\Template\Context; | |
use Magento\Backend\Block\Widget\Form\Element; | |
use Magento\Cms\Model\Wysiwyg\Config; | |
use Magento\Framework\Data\Form\Element\AbstractElement; | |
use Magento\Framework\Data\Form\Element\Factory; | |
class Wysiwyg extends Element | |
{ | |
/** | |
* @var Factory | |
*/ | |
protected $_elementFactory; | |
/** | |
* @param Context $context | |
* @param Factory $elementFactory | |
* @param array $data | |
* @param Config $config | |
*/ | |
public function __construct(Context $context, Factory $elementFactory, Config $config, array $data = []) | |
{ | |
$this->_elementFactory = $elementFactory; | |
$this->_wysiwygConfig = $config; | |
parent::__construct($context, $data); | |
} | |
/** | |
* Prepare chooser element HTML. | |
* | |
* @param AbstractElement $element Form Element | |
* | |
* @return AbstractElement | |
*/ | |
public function prepareElementHtml(AbstractElement $element) | |
{ | |
$input = $this->_elementFactory->create('editor', ['data' => $element->getData()]) | |
->setId($element->getId()) | |
->setClass('widget-option input-textarea admin__control-text') | |
->setWysiwyg(true) | |
->setConfig($this->_wysiwygConfig->getConfig(['add_variables' => false, 'add_widgets' => false])) | |
->setForceLoad(true) | |
->setForm($element->getForm()); | |
if ($element->getRequired()) { | |
$input->addClass('required-entry'); | |
} | |
$element->setData('after_element_html', $this->_getAfterElementHtml().$input->getElementHtml()); | |
return $element; | |
} | |
/** | |
* @param AbstractElement $element | |
*/ | |
protected function getWysiwygConfig(AbstractElement $element) | |
{ | |
$config = $this->_wysiwygConfig->getConfig(); | |
$config->setData('add_variables', false); | |
$config->setData('add_widgets', false); | |
$config->addData([ | |
'settings' => [ | |
'mode' => 'exact', | |
'elements' => $element->getHtmlId(), | |
'theme_advanced_buttons1' => 'bold,italic,underline', | |
'theme_advanced_buttons2' => null, | |
'theme_advanced_buttons3' => null, | |
'theme_advanced_buttons4' => null, | |
], | |
]); | |
return $config; | |
} | |
/** | |
* @return string | |
*/ | |
protected function _getAfterElementHtml() | |
{ | |
$html = <<<HTML | |
<style> | |
.admin__field-control.control .control-value { | |
display: none !important; | |
} | |
</style> | |
HTML; | |
return $html; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment