2

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

Muhammad Anas
1,4673 gold badges13 silver badges33 bronze badges
asked May 9, 2017 at 5:50

2 Answers 2

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);
answered May 9, 2017 at 6:17
5
  • Uncaught Error: Cannot instantiate interface Zend_Filter_Interface Commented Aug 29, 2018 at 7:07
  • @KetanBorada You need to add di.xml. Please check my updated answer Commented Aug 29, 2018 at 8:31
  • i'm still getting same error are you sure this is right? prntscr.com/ko585c Commented Aug 29, 2018 at 8:41
  • can you show by objectmanager? Commented Aug 29, 2018 at 8:55
  • @PrincePatel I am also working same task can you pls help me magento.stackexchange.com/q/330082/57334 Commented Jan 27, 2021 at 13:23
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.

answered May 9, 2017 at 6:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.