6

I add wysiwyng editor in admin Ui form.

<field name="content">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="sortOrder" xsi:type="number">30</item>
            <item name="label" translate="true" xsi:type="string">Content</item>
            <item name="formElement" xsi:type="string">wysiwyg</item>
            <item name="template" xsi:type="string">ui/form/field</item>
            <item name="source" xsi:type="string">popup</item>
            <item name="wysiwyg" xsi:type="boolean">true</item>
            <item name="dataScope" xsi:type="string">content</item>
            <item name="validation" xsi:type="array">
                <item name="required-entry" xsi:type="boolean">true</item>
            </item>
        </item>
    </argument>
</field>

It saves data in the following format in database, that' ok for me:

enter image description here

But when I get data in frontend, it shows this text instead of an image. I want to convert all {{ }} format to HTML like default CMS page, static block functionality.

8

Let's say the block that displays your content is called Vendor\Module\Block\Entity.
Add a dependency Zend_Filter_Interface to your block like this:

protected $templateProcessor;
public function __construct(
    ...
    \Zend_Filter_Interface $templateProcessor,
    .....
) {
    ....
    $this->templateProcessor = $templateProcessor;
    ....
}

Then create inside the block class a method like this:

public function filterOutputHtml($string) 
{
    return $this->templateProcessor->filter($string);
}

Then add this in your di.xml file:

<type name="Vendor\Module\Block\Entity"> <!-- change name here to match your block class name -->
    <arguments>
        <argument name="templateProcessor" xsi:type="object">Magento\Widget\Model\Template\Filter</argument>
    </arguments>
</type>

Then in your template you can print the content like this:

<?php 
$content = $[entity]->getContent(); //$[entity] is the name of your class instance that comes from the db.
echo $block->filterOutputHtml($content);
?>
  • Awesome, It's working.....! – Prince Patel Apr 18 '17 at 9:10
  • I have tried in another block it's shows "Cannot instantiate interface Zend_Filter_Interface" error – Prince Patel May 25 '17 at 11:45
  • Make sure you add the same thing in di.xml for your new block. – Marius May 25 '17 at 11:46
  • Yes I forgot to define block in di.xml. it's working fine now. Thank you. – Prince Patel May 25 '17 at 11:52
  • It is also possible to directly set the type of $templateProcessor to \Magento\Widget\Model\Template\Filter in the constructor and then you don't need to set the di.xml (not 100% the same thing, but it does the job). Also don't forget to run php bin/magento setup:di:compile – Yaron U. Feb 5 '19 at 16:40

Your Answer

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

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