2

Why nothing is visible in the customer page in the admin panel?

what is wrong?

InstallData.php

use Magento\Eav\Setup\EavSetup; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Eav\Model\Config; use Magento\Customer\Model\Customer; public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); $eavSetup->addAttribute( \Magento\Customer\Model\Customer::ENTITY, 'suspend_account_customer', [ 'type' => 'varchar', 'label' => 'Suspend Account Customer', 'input' => 'text', 'required' => false, 'visible' => true, 'user_defined' => false, 'position' => 999, 'system' => 0, ] ); $suspend_customer = $this->eavConfig->getAttribute(Customer::ENTITY, 'suspend_account_customer'); $suspend_customer->setData( 'used_in_forms', ['adminhtml_customer'] ); $suspend_customer->save(); } 

4 Answers 4

1

Create your custom module first.

Add below code to setup folder create CustomerSetup.php file at app/code/VendorName/Modulename/Setup/CustomerSetup.php

<?php namespace VendorName\Modulename\Setup; use Magento\Eav\Model\Config; use Magento\Eav\Model\Entity\Setup\Context; use Magento\Eav\Setup\EavSetup; use Magento\Framework\App\CacheInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory; /** * @codeCoverageIgnore * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class CustomerSetup extends EavSetup { /** * EAV configuration * * @var Config */ protected $eavConfig; /** * Init * * @param ModuleDataSetupInterface $setup * @param Context $context * @param CacheInterface $cache * @param CollectionFactory $attrGroupCollectionFactory * @param Config $eavConfig */ public function __construct( ModuleDataSetupInterface $setup, Context $context, CacheInterface $cache, CollectionFactory $attrGroupCollectionFactory, Config $eavConfig ) { $this -> eavConfig = $eavConfig; parent :: __construct($setup, $context, $cache, $attrGroupCollectionFactory); } /** * Gets EAV configuration * * @return Config */ public function getEavConfig() { return $this -> eavConfig; } } 

Create InstallData.php file at app/code/VendorName/Modulename/Setup/InstallData.php add below code to it

<?php namespace VendorName\Modulename\Setup; use Magento\Eav\Setup\EavSetup; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; /** * @codeCoverageIgnore */ class InstallData implements InstallDataInterface { /** * EAV setup factory * * @var EavSetupFactory */ private $eavSetupFactory; /** * Customer setup factory * * @var CustomerSetupFactory */ private $customerSetupFactory; /** * Init * * @param EavSetupFactory $eavSetupFactory * @param CustomerSetupFactory $customerSetupFactory */ public function __construct(EavSetupFactory $eavSetupFactory, CustomerSetupFactory $customerSetupFactory) { $this->eavSetupFactory = $eavSetupFactory; $this->customerSetupFactory = $customerSetupFactory; } /** * {@inheritdoc} * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); /** @var CustomerSetup $customerSetup */ $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); $customerEntity = $this->eavConfig->getEntityType('customer'); $attributeSetId = $customerEntity->getDefaultAttributeSetId(); /** @var $attributeSet AttributeSet */ $attributeSet = $this->attributeSetFactory->create(); $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId); $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'suspend_account_customer', [ 'label' => 'User Note', 'system' => 0, 'position' => 100, 'sort_order' =>100, 'visible' => true, 'note' => '', 'type' => 'text', 'input' => 'textarea', ] ); $attribute = $customerSetup->getEavConfig() ->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'suspend_account_customer') ->addData( [ 'attribute_set_id' => $attributeSetId, 'attribute_group_id' => $attributeGroupId, 'used_in_forms'=> ['adminhtml_customer', 'customer_account_edit'] ] ); $attribute->save(); } } 

Run below command after adding this code to your custom module.

php bin/magento setup:upgrade

Note: If your test module is already exists, please remove entry from setup_module table and then run command

1

you need to create a UI component XML to show the customer custom attribute in backend customer add/edit page.

I assume you are using a custom extension name "Vendor_Module".

steo 1) create customer_form.xml under /app/code/Vendor/Module/view/base/ui_component/

File : customer_form.xml

<?xml version="1.0" encoding="UTF-8"?> <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd"> <fieldset name="customer"> <field name="suspend_account_customer" formElement="input"> <settings> <visible>true</visible> </settings> </field> </fieldset> </form> 

steo 2) Flush Magento cache

php bin/magento cache:flush 

Hope your custom field will be after this updates.

0
0

'used_in_forms' must also include customer_account_edit

0

Lots of required dependencies missing in your setup file

Your NameSpace not declared in your InstallData.php

Missing dependencies

Magento\Customer\Setup\CustomerSetupFactory Magento\Customer\Model\Customer Magento\Eav\Model\Entity\Attribute\Set Magento\Eav\Model\Entity\Attribute\SetFactory

also make sure to add the customer attribute to the respective form values

adminhtml_customer - To get the new field in admin customer account edit form adminhtml_customer_address - To get the new field in admin customer address edit form customer_address_edit - To get the new field in storefront customer address edit form customer_register_address - To get the new field in storefront customer address registration form

Please note you also need to change the respective templates to add your customer attribute in the store front.

<?php namespace Vendor\YourModule\Setup; use Magento\Customer\Setup\CustomerSetupFactory; use Magento\Customer\Model\Customer; use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet; use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; /** * @codeCoverageIgnore */ class InstallData implements InstallDataInterface { /** * @var CustomerSetupFactory */ protected $customerSetupFactory; /** * @var AttributeSetFactory */ private $attributeSetFactory; /** * @param CustomerSetupFactory $customerSetupFactory * @param AttributeSetFactory $attributeSetFactory */ public function __construct( CustomerSetupFactory $customerSetupFactory, AttributeSetFactory $attributeSetFactory ) { $this->customerSetupFactory = $customerSetupFactory; $this->attributeSetFactory = $attributeSetFactory; } public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { /** @var CustomerSetup $customerSetup */ $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer'); $attributeSetId = $customerEntity->getDefaultAttributeSetId(); /** @var $attributeSet AttributeSet */ $attributeSet = $this->attributeSetFactory->create(); $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId); $customerSetup->addAttribute(Customer::ENTITY, 'suspend_account_customer', [ 'type' => 'varchar', 'label' => 'Suspend Account Customer', 'input' => 'text', 'required' => false, 'visible' => true, 'user_defined' => true, 'position' =>999, 'system' => 0, ]); $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'suspend_account_customer') ->addData([ 'attribute_set_id' => $attributeSetId, 'attribute_group_id' => $attributeGroupId, 'used_in_forms' => ['adminhtml_customer'],//you can use other forms also ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address'] ]); $attribute->save(); } } 

For more information, you can refer this answer and if it helps upvote that answer not mine.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.