How to create customer attributes magento 2 so it works in running script without install script:-
/**
* @var \Magento\Eav\Setup\EavSetupFactory
*/
protected $eavEavSetupFactory;
public function __construct(
...
\Magento\Eav\Setup\EavSetupFactory $eavEavSetupFactory,
...
)
{
...
$this->eavEavSetupFactory = $eavEavSetupFactory;
...
}
public function createOrUpdateCustomerUDA($value, $input, $type)
{
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->eavEavSetupFactory->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, $value, [
'type' => $type,
'label' => $value,
'input' => $input,
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]);
//add attribute to attribute set
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, $value)
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer'],
]);
$attribute->save();
}
I am using this function in my custom module to create customer attributes, but it give me below error, can please guide me how to pass the $setup
in this function so it's working fine:-
Parse error: syntax error, unexpected '$setup' (T_VARIABLE), expecting ',' or ')' in Customer.php on line 540
Uncaught TypeError: Argument 1 passed to Magento\Setup\Module\DataSetup::__construct()
Magento\Framework\Setup\ModuleDataSetupInterface
Can you try to load this in construct and pass its reference as argument for setup argument