1

is it possible to call static blocks in the category description of magento 2?

I don´t want to use the "Add CMS Block" DropDown under the description field.

It is necessary to call the blocks in the description field.

I would like to call blocks like sliders: {{revslider alias="fullslider-en"}}

or product lists:

{{block class="Infortis\Base\Block\Product\ProductList\Featured" template="product/list.phtml" category_id="13"}}

Any ideas?

Many thanks in advance Andi

2

Override View.php block

vendor/magento/module-catalog/Block/Category/View.php

Add class \Zend_Filter_Interface in your block to filter output to html

...
protected $templateProcess;
...

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

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

}

In your di.xml

app/code/Vendor/Module/etc/di.xml

<type name="Vendor\Module\Block\Category\View">
    <arguments>
        <argument name="templateProcessor" xsi:type="object">Magento\Widget\Model\Template\Filter</argument>
    </arguments>
</type>

Now call filterOutputHtml() to description.phtml

Override description.phtml to your theme

vendor/magento/module-catalog/view/frontend/templates/category/description.phtml

<?php 

//Get category description
$_description = $block->getCurrentCategory()->getDescription();

//Convert block {{ }} to html
<?php echo $block->filterOutputHtml($_description);
  • Uncaught Error: Cannot instantiate interface Zend_Filter_Interface – Ketan Borada Aug 29 '18 at 7:07
  • @KetanBorada You need to add di.xml. Please check my updated answer – Prince Patel Aug 29 '18 at 8:31
  • i'm still getting same error are you sure this is right? prntscr.com/ko585c – Ketan Borada Aug 29 '18 at 8:41
  • can you show by objectmanager? – Ketan Borada Aug 29 '18 at 8:55
0

You need to override core functionality to show widget options then you can insert static block using widget interface.

Create/Edit your di.xml and define a preference:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for='Magento\Catalog\Block\Adminhtml\Helper\Form\Wysiwyg\Content' type='[Vendor]\[Module]\Block\Adminhtml\Helper\Form\Wysiwyg\ContentRewrite'/>
</config>

File Magento\Catalog\Block\Adminhtml\Helper\Form\Wysiwyg\Content

protected function _prepareForm()
{
    /** @var \Magento\Framework\Data\Form $form */
    $form = $this->_formFactory->create(
        [
            'data' => ['id' => 'wysiwyg_edit_form', 'action' => $this->getData('action'), 'method' => 'post'],
        ]
    );

    $config['document_base_url'] = $this->getData('store_media_url');
    $config['store_id'] = $this->getData('store_id');
    $config['add_variables'] = false;
    $config['add_widgets'] = true; // change this from false to true
    $config['add_directives'] = true;
    $config['use_container'] = true;
    $config['container_class'] = 'hor-scroll';

    $form->addField(
        $this->getData('editor_element_id'),
        'editor',
        [
            'name' => 'content',
            'style' => 'width:725px;height:460px',
            'required' => true,
            'force_load' => true,
            'config' => $this->_wysiwygConfig->getConfig($config)
        ]
    );
    $this->setForm($form);
    return parent::_prepareForm();
}

You may also need to override the block and view file for templateProcess.

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.