Adding a WYSIWYG Editor to Magento Custom Widget
If you’re looking to enhance your Magento custom widget by adding a WYSIWYG (What You See Is What You Get) editor instead of a standard textarea, you’ve come to the right place. In this post, we’ll guide you through the process step-by-step.
Understanding the Current Setup
First, let’s understand what your current setup looks like. Your widget.xml file contains a description parameter with a <type> of textarea:
<parameters>
<description translate="label">
<required>0</required>
<visible>1</visible>
<label>Description</label>
<type>textarea</type>
</description>
</parameters>
The Goal: Incorporating a WYSIWYG Editor
You want to replace this textarea with a WYSIWYG editor. Ideally, the widget.xml would look like this:
<parameters>
<description translate="label">
<required>0</required>
<visible>1</visible>
<label>Description</label>
<type>WYSIWYG</type>
</description>
</parameters>
The Approach: Customizing the Renderer
To achieve this, you’ll need to create a custom renderer that calls Magento’s built-in WYSIWYG editor. Here’s how:
-
Create a new file in your module directory structure:
app/code/YourVendor/YourModule/view/adminhtml/ui_component/widget_form.xml -
Open this new XML file and add the following content:
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui: etc/ui_configuration.xsd">
<argument name="data" xsi:type="array">
<item name="js_config" xsi:type="array">
<item name="provider" xsi:type="string">widget_form.provider</item>
<item name="deps" xsi:type="array">
<item name="targets">wysiwyg_provider</item>
</item>
</item>
</argument>
<settings>
<buttons>
<button class="Magento\Backend\Model\Widget\Button" id="save_wysiwyg_button"/>
</buttons>
</settings>
<formElements>
<field name="description">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Description</item>
<item name="visible" xsi:type="boolean">true</item>
<item name="required" xsi:type="boolean">false</item>
<item name="formElement" xsi:type="string">Magento\Wysiwyg\Ui\Component\Form\Fieldset</item>
</item>
</argument>
</field>
</formElements>
</form>
-
Save the file.
-
Now, you need to update your
widget.xmlfile to reference this new renderer:
<parameters>
<description translate="label">
<required>0</required>
<visible>1</visible>
<label>Description</label>
<type>text</type>
<renderer class="Vendor\Module\Block\Adminhtml\Form\Renderer\Wysiwyg" />
</description>
</parameters>
-
Create the renderer file:
app/code/Vendor/Module/Block/Adminhtml/Form/Renderer/Wysiwyg.php -
Implement the
getConfigurationmethod in this new renderer class:
public function getConfiguration()
{
return [
'template' => 'Magento_Wysiwyg::field.phtml',
'componentType' => 'Magento_Wysiwyg',
'config' => [
'source_model' => 'Vendor\Module\Model\Widget\Config\Source\Description',
],
];
}
-
Create a source model:
app/code/Vendor/Module/Model/Widget/Config/Source/Description.php -
Implement the
getOptionsmethod in this new source model:
public function getOptions()
{
return [
'description' => 'Description',
];
}
Conclusion
Following these steps should allow you to successfully add a WYSIWYG editor to your Magento custom widget.
Remember, the key lies in creating a custom renderer that leverages Magento’s built-in WYSIWYG functionality. By referencing this renderer in your widget.xml file, you can achieve the desired enhancement.
If you found this guide helpful and want more detailed tutorials on Magento development, feel free to explore our blog at Laravel Company. We regularly publish articles covering Magento customization, performance optimization, and best practices.