Pregunta

I want to add TextArea in Admin Edit category Page.

How to do this.

¿Fue útil?

Solución

Two steps to create category attribute,

  1. Add a InstallData.php in the below path

app/code/vendor/ModuleName/Setup/InstallData.php

then add the below code to it

<?php
namespace Vendor\ModuleName\Setup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
class InstallData implements InstallDataInterface
{
 protected $eav_setup;
 protected $connection;
 public function __construct(EavSetupFactory $eavSetupFactory,
    \Magento\Framework\App\ResourceConnection $connection,
    \Magento\Eav\Model\Config $eavConfig
 ) {
     $this->eav_setup_factory = $eavSetupFactory;
     $this->connection = $connection->getConnection();
     $this->eav_config = $eavConfig;
 }
 public function install(
    ModuleDataSetupInterface $setup,
    ModuleContextInterface $context
 ) {
    $setup->startSetup();

    //create Category Attributes
    $this->createCategoryAttributes($setup);

    $setup->endSetup();
 }
 protected function createCategoryAttributes($setup)
 {
    $eav_setup = $this->eav_setup_factory->create(['setup' => $setup]);
    $eav_setup->addAttribute(
        \Magento\Catalog\Model\Category::ENTITY,
        'attribute_code',
        [
            'type' => 'text',
            'label' => 'Category Attribute Name',
            'input' => 'textarea',
            'sort_order' => 420,
            'source' => '',
            'global' => ScopedAttributeInterface::SCOPE_STORE,
            'visible' => true,
            'required' => false,
            'user_defined' => false,
            'default' => null,
            'group' => 'Custom Attributes',
            'backend' => ''
        ]
    );
 }
}
  1. Add category_form.xml in the below path to make it visible in admin

app/code/Vendor/ModuleName/view/adminhtml/ui_component/category_form.xml

then add the below code to it

<?xml version="1.0" ?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
  <fieldset name="custom_attribute">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string">Custom Attributes</item>
            <item name="collapsible" xsi:type="boolean">true</item>
            <item name="sortOrder" xsi:type="number">100</item>
        </item>
    </argument>
    <field name="attribute_code">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="required" xsi:type="boolean">false</item>
                <item name="validation" xsi:type="array">
                    <item name="required-entry" xsi:type="boolean">false</item>
                </item>
                <item name="sortOrder" xsi:type="number">40</item>
                <item name="dataType" xsi:type="string">string</item>
                <item name="formElement" xsi:type="string">textarea</item>
                <item name="label" translate="true" xsi:type="string">Category Attribute Name</item>
            </item>
        </argument>
    </field>
   </fieldset>
</form>

After done this run the below command

php bin/magento setup:upgrade
php bin/magento cache:flush

NOTE : Make sure your module isn't installed, if installed then delete it from setup module or run the below query in the sql console

DELETE from setup_module where module = 'Vendor_ModuleName';

Hope this helps.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange
scroll top