Magento-2 Style attribute value on frontend
-
04-10-2020 - |
Question
I have created attribute using install script
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'mydemo',
[
'type' => 'text',
'backend' => '',
'frontend' => 'Vendor\ModuleName\Model\Frontend\Style',
'label' => 'My Demo',
'input' => 'text',
'class' => '',
'source' => '',
'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => 0,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => true,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => ''
]
);
My frontend model file :
namespace Vendor\ModuleName\Model\Frontend;
class Style extends \Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend
{
public function __construct(
\Magento\Eav\Model\Entity\Attribute\Source\BooleanFactory $attrBooleanFactory
) {
parent::__construct($attrBooleanFactory);
}
public function getValue(\Magento\Framework\DataObject $object)
{
$data = '';
$value = parent::getValue($object);
return "<b>{$value}</b>";
}
}
I want to show attribute value bold on frontend but it's not working. Can anyone suggest me how to achieve this? Any help would be greatly appreciated. Thanks!
Solution
I got solution as below:
namespace Vendor\ModuleName\Model\Frontend;
class Style extends \Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend
{
public function getValue(\Magento\Framework\DataObject $object)
{
$this->getAttribute()->setData(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::IS_HTML_ALLOWED_ON_FRONT, 1);
$data = '';
$value = parent::getValue($object);
return "<b>". $value ."</b>";
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange