3

How do you add a WYSIWYG editor to a custom form which you create through a block? I have found this article for doing this through UI components but need this for a block.

I've tried the following:

$fieldset->addField(
            'content',
            'editor',
            [
                'name' => 'content',
                'label' => __('Body'),
                'title' => __('Body'),
                'rows' => '5',
                'cols' => '30',
                'wysiwyg' => true,
                'required' => true
            ]
        );

4 Answers 4

17

Discovered I was missing the Config setting:

/**
 * @var \Magento\Cms\Model\Wysiwyg\Config
 */
protected $_wysiwygConfig;

/**
 * @param \Magento\Backend\Block\Template\Context $context
 * @param \Magento\Framework\Registry $registry
 * @param \Magento\Framework\Data\FormFactory $formFactory
 * @param \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig
 * @param \Magento\Store\Model\System\Store $systemStore
 * @param array $data
 */
public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig,
    array $data = []
) {
    $this->_wysiwygConfig = $wysiwygConfig;
    parent::__construct($context, $data);
}

Meaning I could do the following:

$fieldset->addField(
    'content',
    'editor',
    [
        'name' => 'content',
        'label' => __('Body'),
        'title' => __('Body'),
        'rows' => '5',
        'cols' => '30',
        'wysiwyg' => true,
        'config' => $this->_wysiwygConfig->getConfig(),
        'required' => true
    ]
);

Type must be set to editor and wysiwyg set to true for this to work.

2
  • magezon page builder is not working for this editor. Commented Sep 2, 2020 at 7:10
  • I'm getting Uncaught ReferenceError: require is not defined on Magento 2.4 are you getting the same error ? Commented Dec 28, 2021 at 23:05
3

It is very simple dont do all this things just put below code in app\code\NameSpace\Module\view\adminhtml\layout\module_module_form.xml

<field name="content">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string"/>
                    <item name="formElement" xsi:type="string">wysiwyg</item>
                    <item name="source" xsi:type="string">block</item>
                    <item name="wysiwyg" xsi:type="boolean">true</item>
                    <item name="dataScope" xsi:type="string">content</item>
                    <item name="additionalClasses" xsi:type="string">admin__field-wide</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                    </item>
                </item>
            </argument>
        </field>
2
  • see below ansvwer
    – Parth
    Commented Jul 14, 2017 at 7:46
  • works but shows in full length
    – Avesh Naik
    Commented May 6, 2019 at 9:55
3

@Avesh Naik you can use below code to add the wysiwyg field in UI Form, it will add field with the form width, you will not face the fullwidth issue with field.

    <field name="front_message2" sortOrder="10" formElement="wysiwyg" template="ui/form/field">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="source" xsi:type="string">lrpricerules</item>
                <item name="wysiwygConfigData" xsi:type="array">
                    <item name="is_pagebuilder_enabled" xsi:type="boolean">false</item>
                    <item name="toggle_button" xsi:type="boolean">true</item>
                    <item name="height" xsi:type="string">150px</item>
                    <item name="add_variables" xsi:type="boolean">true</item>
                    <item name="add_widgets" xsi:type="boolean">true</item>
                    <item name="add_images" xsi:type="boolean">true</item>
                    <item name="add_directives" xsi:type="boolean">true</item>
                </item>
            </item>
        </argument>
        <settings>
            <label translate="true">Front end message2</label>
            <dataScope>front_message2</dataScope>
        </settings>
        <formElements>
            <wysiwyg>
                <settings>
                    <rows>5</rows>
                    <wysiwyg>true</wysiwyg>
                </settings>
            </wysiwyg>
        </formElements>
    </field>

upvote for me if its useful for you.

0
<field name="description_custom" template="ui/form/field" formElement="wysiwyg">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="wysiwygConfigData" xsi:type="array">
                        <item name="height" xsi:type="string">100px</item>
                        <item name="add_variables" xsi:type="boolean">false</item>
                        <item name="add_widgets" xsi:type="boolean">false</item>
                        <item name="add_images" xsi:type="boolean">true</item>
                        <item name="add_directives" xsi:type="boolean">true</item>
                    </item>
                    <item name="source" xsi:type="string">category</item>
                </item>
            </argument>
            <settings>
                <validation>
                    <rule name="required-entry" xsi:type="boolean">false</rule>
                </validation>
                <dataType>text</dataType>
                <label translate="true">Description Custom</label>
                <dataScope>description_custom</dataScope>
                <notice translate="true">Note: Keyboard shortcut to activate editor help : Alt + 0 (Windows) or &#x2325;0 (MacOS)</notice>
            </settings>
            <formElements>
                <wysiwyg class="Magento\Catalog\Ui\Component\Category\Form\Element\Wysiwyg">
                    <settings>
                        <rows>8</rows>
                        <wysiwyg>true</wysiwyg>
                    </settings>
                </wysiwyg>
            </formElements>
        </field>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.