Created
September 26, 2016 15:22
-
-
Save zzpaul/bebd89f4665a8ac81db5e3c4ac953ab7 to your computer and use it in GitHub Desktop.
Magento 2 - Add custom attribute in customer registration form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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