Magento 2 – How to Add WYSIWYG Functionality in Custom Module

adminmagento2modulewysiwyg

Today I'm using the textarea to fields in my custom module, but I want to improve with the WYSIWYG.

How could I use WYSIWYG in a custom field in the admin configuration panel that my module creates?

Thanks

Best Answer

Add following code in your file.

app/code/Namespace/Modulename/Block/Adminhtml/ModuleFolder/Edit/Tab/Main.php

protected $wysiwyg;

public function __construct(
    \Magento\Cms\Model\Wysiwyg\Config $wysiwyg
    ){
       $this->wysiwyg = $wysiwyg;
     }

$fieldset->addField(
            'fieldname',
            'editor',
            [
                'name' => 'fieldname',
                'label' => __('Fieldname'),
                'title' => __('Fieldname'),
                 'config'    => $this->wysiwyg->getConfig(),
                'wysiwyg'   => true
            ]
        );

Done. You can see WYSIWYG in your Admin form of your Custom Module.