WYSIWYG is an HTML editor to easily play with the content editing. WYSIWYG allows adding and formatting the content providing “What You See Is What You Get ” view. One can see the end result that’ll be displayed in the editing phase. Save yourself from the hassle of HTML programming for enhancing the appearance of frontend!
Magento 2, designed to be as user-friendly as possible, has WYSIWYG as the default editor. ? But sometimes, you need to add WYSIWYG editor in Magento 2 Admin Form to collect data.
Adding WYSIWYG editor in Magento 2 backend eases the task of website development. What more luxury does a developer wish for?
So, here’s the code for you, ready to be implemented in the admin backend!
Code to Add WYSIWYG Editor in Magento 2 Admin Form
Add the following code in your block file:
protected $wysiwygConfig; public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig, array $data = [] ) { $this->wysiwygConfig = $wysiwygConfig; parent::__construct($context, $data); } Add field code $fieldset->addField( 'content', 'editor', [ 'name' => 'content', 'label' => __('Body'), 'title' => __('Body'), 'rows' => '5', 'cols' => '30', 'wysiwyg' => true, 'config' => $this->wysiwygConfig->getConfig(), 'required' => true ] );
Follow the above method and the content editing is as easy as ABC!