3

In Magento 2 we can add widgets to CMS pages and CMS blocks with a directive like this (assuming we created this custom widget with a module before):

{{widget type="Vendor\Module\Block\Widget\CustomWidget" some_addtribute="some-value"}}

When we add the same widget code in product or category description it is not rendered. So the output is the text string from above.

How can we extend Magento 2 in a way that it will render widgets in product or category descriptions as well?

4

Instead of instantiating the object manager and filterprovider etc. in your template, consider this:

In your block class (Vendor\Module\Block\Widget\CustomWidget) include the dependencies in your constructor:

/**
 * Example constructor.
 * @param \Magento\Cms\Model\Template\FilterProvider $filterProvider
 * @param \Magento\Catalog\Helper\Output $catalogOutputHelper
 * @param \Magento\Framework\View\Element\Template $context
 * @param array $data
 */
public function __construct(
    \Magento\Cms\Model\Template\FilterProvider $filterProvider,
    \Magento\Catalog\Helper\Output $catalogOutputHelper,
    \Magento\Framework\View\Element\Template $context,
    array $data = []
) {
    $this->filterProvider = $filterProvider;
    $this->catalogOutputHelper = $catalogOutputHelper;
    \Magento\Framework\View\Element\Template::__construct($context, $data);
}

Now in your block class add a method to filter the input:

/**
 * @param string $description
 * @return string
 */
public function getFilteredContent(string $description)
{
    return $this->filterProvider
        ->getBlockFilter()
        ->filter(
            $this->catalogOutputHelper->categoryAttribute(
                $this->getCurrentCategory(),
                $description,
                'description'
            )
        );
}

I assume that getCurrentCategory() is a method you've already created.

Now in your template, you can simply do:

echo $block->getFilteredDescription($_description);

So now your template is neat and tidy and all dependencies and business logic are separated to you block class, as it should.

  • 1
    Thx for the explanation and the implementation example! – Jey DWork Mar 2 '17 at 15:19
  • How can i achive this in category description?i have call static block within description but it is not working for me prntscr.com/ko5qyj – Ketan Borada Aug 29 '18 at 9:20
1

Meanwhile I can answer that myself.

While there might be better approaches one of the easiest is to just get an instance of FilterProvider (and the StoreMangerInterface) in the template where the widget should get rendered and pass the output through the block filter of the FilterProvider.

So for the category description this would for example be the template vendor\magento\module-catalog\view\frontend\templates\category\description.phtml which you should copy to your custom theme: app\design\frontend\Vendor\theme\Magento_Catalog\templates\category\description.phtml.

In it you can now replace

/* @escapeNotVerified */ echo $this->helper('Magento\Catalog\Helper\Output')->categoryAttribute($block->getCurrentCategory(), $_description, 'description') ;

with something like this:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$filterProvider = $objectManager->create('\Magento\Cms\Model\Template\FilterProvider');
$storeManager = $objectManager->create('\Magento\Store\Model\StoreManagerInterface');

echo $filterProvider->getBlockFilter()
     ->setStoreId($storeManager->getStore()->getId())
     ->filter(
         $this->helper('Magento\Catalog\Helper\Output')->categoryAttribute($block->getCurrentCategory(), $_description, 'description')
      );

And you can do similar things in all templates where you want something to render widgets.

  • 1
    Don't use the object manager, but dependency injection instead – Giel Berkers Feb 23 '17 at 14:13
  • That sounds like a better solution. Can you elaborate it? I. e. show how it is done with DI and give a reason why it is the better? I would be really interested in it and obviously switch the accepted answer to yours then. – Jey DWork Feb 26 '17 at 4:45

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.