How to add Show / Hide Editor for custom attribute in magento 2
-
14-04-2021 - |
Solución
For this custom attribute, You should enable wysiwyg editor
.
Solution1: from admin
If your attribute product attribute then goto admin>Store>Attribute>Product
Select your attribute goto tab>Storefront Properties>Enable WYSIWYG
Make this value to Yes
Solution2 : Using UpgradeData script
If you create this custom attribute using setup script then also using create an updatedata script then using make 'wysiwyg_enabled' => true
for attribute.
UpgradeData setup file is:
================
<?php
namespace [VendorName]\[MOduleName]\Setup;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
/**
* Upgrade Data script
* @codeCoverageIgnore
*/
class UpgradeData implements UpgradeDataInterface
{
/**
* Category setup factory
*
* @var CategorySetupFactory
*/
private $categorySetupFactory;
/**
* EAV setup factory
*
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* Init
*
* @param CategorySetupFactory $categorySetupFactory
* @param EavSetupFactory $eavSetupFactory
*/
public function __construct(CategorySetupFactory $categorySetupFactory, EavSetupFactory $eavSetupFactory)
{
$this->categorySetupFactory = $categorySetupFactory;
$this->eavSetupFactory = $eavSetupFactory;
}
/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
/** @var EavSetup $eavSetup */
$eavSetup= $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->updateAttribute(
ProductAttributeInterface::ENTITY_TYPE_CODE,
'[YOUR_ATTRIBUTE_CODE]',
[
'wysiwyg_enabled' => true
]
);
$setup->endSetup();
}
/**
}
Otros consejos
In case Amit Bera solution doesn't work. Try mine:
Instead of:
$eavSetup->updateAttribute(
ProductAttributeInterface::ENTITY_TYPE_CODE,
'[YOUR_ATTRIBUTE_CODE]',
[
'wysiwyg_enabled' => true
]
);
use:
$eavSetup->updateAttribute(
ProductAttributeInterface::ENTITY_TYPE_CODE,
'[YOUR_ATTRIBUTE_CODE]',
'is_wysiwyg_enabled',
false
);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange