Search code examples
magento2.4

How can I create the profile image customer attribute using data patch magento 2?


I need to create the customer profile image attribute. Anyone can please help how can I create the customer profile image attribute. I need to create the custom attribute in the custom module and I'm using the data patch file.

 public function apply()
{
    $this->moduleDataSetup->getConnection()->startSetup();
    /** @var CustomerSetup $customerSetup */
    $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
    $customerEntity = $customerSetup->getEavConfig()->getEntityType(Customer::ENTITY);
    $attributeSetId = $customerEntity->getDefaultAttributeSetId();
    
    /** @var $attributeSet Set */
    $attributeSet = $this->attributeSetFactory->create();
    $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
    
    $customerSetup->addAttribute(
        Customer::ENTITY,
        'profile_image',
        [
            'label' => 'Profile Image',
            'input' => 'text',
            'type' => 'file',
            'source' => '',
            'required' => true,
            'position' => 333,
            'visible' => true,
            'system' => false,
            'is_used_in_grid' => true,
            'is_visible_in_grid' => true,
            'is_filterable_in_grid' => true,
            'is_searchable_in_grid' => false,
            'backend' => ''
        ]
    );
    
    $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'profile_image');
    $attribute->addData([
        'used_in_forms' => [
            'adminhtml_customer',
            'adminhtml_checkout',
            'customer_account_create',
            'customer_account_edit'
        ]
    ]);
    $attribute->addData([
        'attribute_set_id' => $attributeSetId,
        'attribute_group_id' => $attributeGroupId
    
    ]);
    $attribute->save();

    $this->moduleDataSetup->getConnection()->endSetup();
}

When I'm executing the s:up command then it's showing the "Invalid column data type file" error. Please let me know how can I fix this.


Solution

  • The 'type' parameter must be one of the following:
    datetime, decimal, int, text or varchar

    Those options corresponds to the customer_entity_* database tables.

    Also, there is no standard file 'input' field for customer attributes. You'll need to implement that field input type.