You have to keep below code inside your UpgradeData.php
file,
<?php
/**
* Copyright © 2015 Clounce. All rights reserved.
* See LICENSE.txt for license details.
*/
namespace {Vendor}\{Module}\Setup;
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;
use Magento\Customer\Model\Customer;
class UpgradeData implements UpgradeDataInterface
{
public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}
/**
* Installs data for a module
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$attributes = [
'test'=> [
"type" => "varchar",
"label" => "Your custom label",
"input" => "text",
'sort_order' => 100,
"required" => false,
'position' => 100,
'system' => false,
//'validate_rules' => 'a:2:{s:15:"max_text_length";i:255;s:15:"min_text_length";i:1;}',
],
];
foreach ($attributes as $code => $options) {
$eavSetup->addAttribute(
Customer::ENTITY,
$code,
$options
);
}
$this->installCustomerForms($eavSetup);
}
/**
* Add customer attributes to customer forms
*/
public function installCustomerForms(EavSetup $eavSetup)
{
$customer = (int)$eavSetup->getEntityTypeId(\Magento\Customer\Model\Customer::ENTITY);
/**
* @var ModuleDataSetupInterface $setup
*/
$setup = $eavSetup->getSetup();
$attributeIds = [];
$select = $setup->getConnection()->select()->from(
['ea' => $setup->getTable('eav_attribute')],
['entity_type_id', 'attribute_code', 'attribute_id']
)->where(
'ea.entity_type_id IN(?)',
[$customer]
);
foreach ($eavSetup->getSetup()->getConnection()->fetchAll($select) as $row) {
$attributeIds[$row['entity_type_id']][$row['attribute_code']] = $row['attribute_id'];
}
$data = [];
$attributes = [
'test'=> [
"type" => "varchar",
"label" => "Your custom label",
"input" => "text",
'sort_order' => 100,
"required" => false,
'position' => 100,
'system' => false,
//'validate_rules' => 'a:2:{s:15:"max_text_length";i:255;s:15:"min_text_length";i:1;}',
],
];
foreach ($attributes as $attributeCode => $attribute) {
$attributeId = $attributeIds[$customer][$attributeCode];
$attribute['system'] = isset($attribute['system']) ? $attribute['system'] : true;
$attribute['visible'] = isset($attribute['visible']) ? $attribute['visible'] : true;
if ($attribute['system'] != true || $attribute['visible'] != false) {
$usedInForms = ['customer_account_create','adminhtml_customer','checkout_register','customer_account_edit','adminhtml_checkout'];
foreach ($usedInForms as $formCode) {
$data[] = ['form_code' => $formCode, 'attribute_id' => $attributeId];
}
}
}
if ($data) {
$setup->getConnection()->insertOnDuplicate($setup->getTable('customer_form_attribute'), $data);
}
}
}
Remove var folder from root and run command,
php bin/magento setup:upgrade
and
php bin/magento indexer:reindex
Check again. Its working.
Best Answer
Read More At: Mageprince Blog
You only need to run
setup:upgrade
command1. When you made changes in Setup script(InstallData, InstallSchema,
UpgradeData, UpgradeSchema, ...)
2. If you install Magento first time.
3. At the time of new module installation
4. After upgrade magento version.
1) Check module version in
setup_module
table2) If version not available or new version added in module.xml, It will run setup script and add latest version number in table
If you made changes in HTML, CSS, JS, ... files you need to delete particular changed files from
pub/static
folder or run this commandIf you made changes like add new dependency in __construct() or changes in
di.xml
, you need to delete changed files fromvar/generation
folder or run this commandIf you made changes in admin configuration, layout xml, ui component, phtml, ... files you need to only clean or flush cache
EDIT:
You can run cache flush by only typing
c:f
without using the full commandphp bin/magento c:f
More info At: Create Short Forms Of Magento 2 Commands