0

I am having trouble implementing a WYSIWYG editor inside a widget.

Preconditions (*)

1. Magento 2.3.1
2. Add a Wysiwyg element to a widget

app/code/{Vendor}/{Module}/etc/widget.xml

<parameter xsi:type="block" name="body_text" visible="true" sort_order="10">
          <label translate="true">Text</label>
         <block class="{Vendor}\{Module}\Block\Adminhtml\Widget\Type\Wysiwyg"/>
   </parameter>

app/code/{Vendor}/{Module}/Block/Adminhtml/Widget/Type/Wisiwyg

<?php
namespace {Vendor}\{Module}\Block\Adminhtml\Widget\Type;
Class Wysiwyg extends \Magento\Backend\Block\Template
{
    /**
     * @var \Magento\Cms\Model\Wysiwyg\Config
     */
    protected $wysiwygConfig;
    /**
     * @var \Magento\Framework\Data\Form\Element\Factory
     */
    protected $factoryElement;
    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Framework\Data\Form\Element\Factory $factoryElement
     * @param \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Data\Form\Element\Factory $factoryElement,
        \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig,
        $data = []
    ) {
        $this->factoryElement = $factoryElement;
        $this->wysiwygConfig = $wysiwygConfig;
        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)
    {
        $editor = $this->factoryElement->create('editor', ['data' => $element->getData()])
            ->setLabel('')
            ->setForm($element->getForm())
            ->setWysiwyg(true)
            ->setConfig(
                $this->wysiwygConfig->getConfig([
                    'add_variables' => false,
                    'add_widgets' => false,
                    'add_images' => false
                ])
            );
        if ($element->getRequired()) {
            $editor->addClass('required-entry');
        }
        $element->setData('after_element_html', $editor->getElementHtml());
        $element->setValue(''); // Hides the additional label that gets added.
        return $element;
    }
}

Steps to reproduce (*)

1. Create a new CMS Page
2. Add a custom widget to the page with at least one element that implements the Wysiwyg editor.
3. Add an image or hyperlink to this WYSIWYG element and add the widget

Expected result (*)

1. The widget gets correctly added to the CMS page

Actual result (*)

1. The following error is thrown
Screen Shot 2019-06-20 at 09 26 56

Your Answer

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

Browse other questions tagged or ask your own question.