0

I am trying to create a customer custom attribute in Magento 2, but it's throwing an error.

Fatal error: Call to a member function create() on null in Custom\Setup\InstallData.php on line 47

For creating customer custom attribute I have written the code below.

<?php
namespace Learning\Custom\Setup;


use Magento\Customer\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{

    /**
     * Customer setup factory
     *
     * @var CustomerSetupFactory
     */
    private $customerSetupFactory;


    /**
     * @var \Magento\Eav\Model\Entity\Attribute\SetFactory
     */
    protected $attributeSetFactory;

    /**
     * @param Setup\CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
    {
        $this->customerSetupFactory = $customerSetupFactory;
    }

    /**
     * {@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);


        $customerSetup->addAttribute(Customer::ENTITY, 'custom_name', [
            'type' => 'varchar',
            'label' => 'Custom Name',
            'input' => 'text',
            'class' => '',
            'source' => '',
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
            /*Scope of your attribute */
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'position' =>999,
            'system' => 0,
        ]);

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

        $attribute->save();
    }


}

Could you please suggest where the problem might be?

1

Check my below suggestion code.

<?php
namespace Learning\Custom\Setup;

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;

class InstallData implements InstallDataInterface
{

    /**
     * @var CustomerSetupFactory
     */
    protected $customerSetupFactory;

    /**
     * @var AttributeSetFactory
     */
    private $attributeSetFactory;

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


    /**
     * {@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);



        $customerSetup->addAttribute(Customer::ENTITY, 'custom_name', [
            'type' => 'varchar',
            'label' => 'Custom Name',
            'input' => 'text',
            'class' => '',
            'source' => '',
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
            /*Scope of your attribute */
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'position' =>999,
            'system' => 0,
        ]);

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

        $attribute->save();
    }


}
  • isn't this what I answered 15 minutes ago? – Marius Nov 7 '16 at 7:18
  • I have added completed script code with use class. – Suresh Chikani Nov 7 '16 at 7:20
1

I assume this is line 47: $attributeSet = $this->attributeSetFactory->create();.
If it is, then the problem is that $this->attributeSetFactory is alwasy null. You declared it in your class, but it never receives a value.
You should change the constructor to look like this:

public function __construct(
    \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory,
    \Magento\Eav\Model\Entity\Attribute\SetFactory $attributeSetFactory
) {
    $this->customerSetupFactory = $customerSetupFactory;
    $this->attributeSetFactory  = $attributeSetFactory;
}

And clear var/generation.

  • Error: The configuration parameter "formElement" is a required for "approved_attribute" field. – Bojjaiah Nov 7 '16 at 7:20

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.