4

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!

3

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>";
    }
}
  • 2
    Maybe would be better to move that to an update script: $eavSetup->updateAttribute( \Magento\Catalog\Model\Product::ENTITY, 'mydemo', [ 'is_html_allowed_on_front' => 1, ] ); – Mehdi Jul 26 '17 at 15:51

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.