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 ?

asked Sep 14, 2018 at 10:40

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.

answered Sep 14, 2018 at 10:50
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. Commented Sep 14, 2018 at 11:15
  • I have updated my answer. Commented Sep 14, 2018 at 11:32
  • updated solution working. But FYI "Method getResource is deprecated" Commented Sep 14, 2018 at 11:47
  • Thanks for information, I have again updated my answer. That you can do without override of block. Commented 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());
answered Sep 14, 2018 at 11:38

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.