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 Answers 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_InterfaceKetan Borada– Ketan Borada2018年08月29日 07:07:32 +00:00Commented Aug 29, 2018 at 7:07
-
@KetanBorada You need to add di.xml. Please check my updated answerPrince Patel– Prince Patel2018年08月29日 08:31:55 +00:00Commented Aug 29, 2018 at 8:31
-
i'm still getting same error are you sure this is right? prntscr.com/ko585cKetan Borada– Ketan Borada2018年08月29日 08:41:42 +00:00Commented Aug 29, 2018 at 8:41
-
can you show by objectmanager?Ketan Borada– Ketan Borada2018年08月29日 08:55:02 +00:00Commented Aug 29, 2018 at 8:55
-
@PrincePatel I am also working same task can you pls help me magento.stackexchange.com/q/330082/57334zus– zus2021年01月27日 13:23:32 +00:00Commented Jan 27, 2021 at 13:23
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.
Explore related questions
See similar questions with these tags.