5

I have created custom attribute of Input Type "Text Area" with WYSIWYG option Enable.

enter image description here

After that add widget code in that product custom attribute input at store back-end.

enter image description here

Its showing same code instead of widget value at store front-end product detail page.
enter image description here

Code in template file.

<?php
$_product = $block->getProduct();
echo $_product->getTest();

Please guide me, why widget/block code not rendering at front-end ?

2 Answers 2

2

Yes it will return same data as that data needs to be filter to get actual html. It's kind of same data as cms page or block have all textarea type attribute data needs to be filter to get actual rendered value.

So do one thing in layout, pass viewmodel argument inside your block for which you have that template. Like this:

<arguments> <argument name="viewModel" xsi:type="object">Namespace\ModuleName\ViewModel\Product</argument> </arguments>

Create a class as specified in view model argument and inside class you need to use :

use Magento\Cms\Model\Template\FilterProvider;

Then declare it in constructor

And create a function to call from template and inside function use it like:

$this->filterProvider->getPageFilter()->filter($_product->getTest());

Then you can call your viewModel inside template like this:

$viewModel = $block->getViewModel();

And call your function from $viewModel.

For detail about viewModel you can check here

You can create an helper and call it directly from template same thing (as stated above for class) can be used in helper class as well.

4
  • Thanks for your answer. I am using template file "app/design/frontend/<Vendor>/<theme>/Magento_Catalog/templates/product/view/description.phtml" to show custom attribute value. According that template file i need to modified or override that block "Magento\Catalog\Block\Product\View\Description"? If yes, then i think its not easy option.
    – Nadeem0035
    Sep 14, 2018 at 11:15
  • I have updated my answer.
    – Kumar M
    Sep 14, 2018 at 11:32
  • updated solution working. But FYI "Method getResource is deprecated"
    – Nadeem0035
    Sep 14, 2018 at 11:47
  • Thanks for information, I have again updated my answer. That you can do without override of block.
    – Kumar M
    Sep 14, 2018 at 11:58
2

You can try with Object Manager but its not recommended to use it directly but dependency injection instead

<?php
$_product = $block->getProduct();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$filterProvider = $objectManager->create('\Magento\Cms\Model\Template\FilterProvider');
echo $filterProvider->getPageFilter()->filter($_product->getTest());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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