0

I have added a customer attribute called "My File". It gets added in customer account edit form. By default, the selected file(My File) is saved under "pub/media/customer" directory. In my case, I want to save those files alone under "pub/media/my_files". How to achieve it? Please help me!

Creating attributes in InstallData.php



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;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\App\ProductMetadataInterface;

/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{
    /**
     * Customer setup factory
     *
     * @var CustomerSetupFactory
     */
    private $customerSetupFactory;

    /**
     * Customer Attribute setup factory
     *
     * @var AttributeSetFactory
     */
    private $attributeSetFactory;

    /**
     * Customer Attribute setup factory
     *
     * @var ProductMetadataInterface
     */
    private $productMetadataInterface;

    /**
     * Init
     *
     * @param CustomerSetupFactory $customerSetupFactory
     * @param AttributeSetFactory $attributeSetFactory
     * @param ProductMetadataInterface $productMetadataInterface
     */
    public function __construct(CustomerSetupFactory $customerSetupFactory, AttributeSetFactory $attributeSetFactory, ProductMetadataInterface $productMetadataInterface)
    {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
        $this->productMetadataInterface = $productMetadataInterface;
    }

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

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

        /** @var $attributeSet AttributeSet */
        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);


            $attributeList['my_file'] = [
                'type' => 'varchar',
                'label' => 'My file',
                'input' => 'file',
                'required' => false,
                'visible' => true,
                'user_defined' => true,
                'sort_order' => 150,
                'position' => 150,
                'system' => 0,
                'validate_rules' => '{"max_file_size":1048576,"file_extensions":"pdf,jpg,png,jpeg,PDF,JPG,PNG,JPEG"}'
            ];

        $attributeList['my_file_link'] = [
            'type' => 'varchar',
            'label' => 'My File Link',
            'input' => 'text',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 160,
            'position' => 160,
            'system' => 0
        ];


        foreach ($attributeList as $key => $value) {
            $customerSetup->addAttribute(Customer::ENTITY, $key, $value);

            //add attribute to attribute set
            $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, $key)
                            ->addData(
                                [
                                'attribute_set_id' => $attributeSetId,
                                'attribute_group_id' => $attributeGroupId,
                                'used_in_forms' => ['customer_account_edit', 'checkout_register', 'adminhtml_customer']
                                ]
                            );

            $attribute->save();
        }
    }
}

2
  • You use any extension ? If not then share your code where you wrote to save attribute value.
    – anonymous
    May 23, 2019 at 7:29
  • Updated the question, please have a look May 23, 2019 at 7:51

1 Answer 1

0

You can modiy code as per you need. I suggest you to observe customer_register_success event. More about observers here.

        $uploaderFactory = $this->uploaderFactory->create(['fileId' => 'my_file']);
        //Magento\MediaStorage\Model\File\UploaderFactory
        $uploaderFactory->setAllowedExtensions(['jpg', 'jpeg', 'png', 'pdf']);
        $imageAdapter = $this->adapterFactory->create();
        // Magento\Framework\Image\AdapterFactory
        $uploaderFactory->setAllowRenameFiles(true);
        $uploaderFactory->setFilesDispersion(true);
        $mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
        $destinationPath = $mediaDirectory->getAbsolutePath('your preferable directory under media');
        $result = $uploaderFactory->save($destinationPath);

        $document = $result['file'];
2
  • Thanks for the support! But if we use, customer_register_success event, it gets triggered after saving the customer details right? In this scenario, already the file gets uploaded in the customer folder, right? May 24, 2019 at 4:42
  • I dont think so. In observer you can change that behaviour.
    – anonymous
    May 24, 2019 at 5:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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