1

Magento 2 how to create customer file attribute,file type must be csv,doc,xlsx

I search and also tried but not working.

Here is my code - InstallData.php

<?php
namespace W3solver\Callforprice\Setup;

use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;


class UpgradeData implements UpgradeDataInterface {

    protected $customerSetupFactory;
    private $attributeSetFactory;

    public function __construct(
    CustomerSetupFactory $customerSetupFactory, AttributeSetFactory $attributeSetFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
    }


    public function upgrade( ModuleDataSetupInterface $setup, ModuleContextInterface $context ) {

        if (version_compare($context->getVersion(), '1.0.4') < 0) {

            $setup->startSetup();
            $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

            $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
            $attributeSetId = $customerEntity->getDefaultAttributeSetId();

            $attributeSet = $this->attributeSetFactory->create();
            $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

            $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'twilio_image', [
                'type' => 'text',
                'label' => 'Twilio Image',
                'input' => 'file',
                "source" => '',
                'required' => false,
                'default' => '0',
                'visible' => true,
                'user_defined' => true,
                'sort_order' => 210,
                'position' => 210,
                'system' => false,
            ]);

            $image = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'twilio_image')
                    ->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => ['adminhtml_customer', 'customer_account_create', 'customer_account_edit'],
            ]);

            $image->save();
            $setup->endSetup();
        } 
    }
}
2
0

You code is fine. If you are below Magento 2.3.1 then you need to change line of code to make it work.

$attributesInfo = [
        'upload_documents' => [
            'label' => 'Twilio Image',
            'type' => 'varchar',
            'input' => 'image',
            'position' => 1000,
            'visible' => true,
            'required' => false,
            'system' => 0,
            'user_defined' => true,
            "source" => 'Magento\Eav\Model\Entity\Attribute\Source\Table',
            'position' => 1000,
            'is_used_in_grid' => true,
            'is_visible_in_grid' => true,
            'is_html_allowed_on_front' => true,
            'visible_on_front' => true
        ]
    ];

Here's github issue change file after compare with git file.

2
  • yes thanks i solved already – sumeet bajaj May 23 '19 at 12:25
  • Good to hear that Magento is stable :) – anonymous May 23 '19 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.