Skip to content

Instantly share code, notes, and snippets.

@zzpaul
Created September 26, 2016 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zzpaul/bebd89f4665a8ac81db5e3c4ac953ab7 to your computer and use it in GitHub Desktop.
Save zzpaul/bebd89f4665a8ac81db5e3c4ac953ab7 to your computer and use it in GitHub Desktop.
Magento 2 - Add custom attribute in customer registration form
<?php
namespace vendor\TestModule\Setup;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetup;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface {
/**
* Customer setup factory
*
* @var \Magento\Customer\Setup\CustomerSetupFactory
*/
private $customerSetupFactory;
public function __construct(CustomerSetupFactory $customerSetupFactory) {
$this->customerSetupFactory = $customerSetupFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
$setup->startSetup();
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerSetup->addAttribute(Customer::ENTITY, 'client_idn', [
'label' => 'Client IDN',
'input' => 'text',
'required' => false,
'sort_order' => 40,
'visible' => true,
'system' => false,
'is_used_in_grid' => true,
'is_visible_in_grid' => true,
'is_filterable_in_grid' => true,
'is_searchable_in_grid' => true]
);
// add attribute to form
/** @var $attribute */
$attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'client_idn');
$attribute->setData('used_in_forms', ['adminhtml_customer', 'customer_account_create']);
$attribute->save();
$setup->endSetup();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment