0

I created a customer EAV attribute to collect the users postcode on registration. I realized Magento already has this feature, so I removed the module. The problem is the module does not seem to be removed.

Here is the code for the attribute. I added a the revert method thinking that it would remove the module from the database when removing the module:

<?php

namespace JR2\ExtraCustomerData\Setup\Patch\Data;

use Magento\Customer\Model\Customer; use Magento\Eav\Model\Config; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\Patch\DataPatchInterface; use Magento\Framework\Setup\Patch\PatchRevertableInterface; use Psr\Log\LoggerInterface;

class AddPostcodeAttribute implements DataPatchInterface, PatchRevertableInterface { private $moduleDataSetup; private $eavSetupFactory; private $logger; private $eavConfig; private $attributeResource;

public function __construct(
    EavSetupFactory $eavSetupFactory,
    Config $eavConfig,
    LoggerInterface $logger,
    \Magento\Customer\Model\ResourceModel\Attribute $attributeResource,
    \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
)
{
    $this-&gt;eavSetupFactory = $eavSetupFactory;
    $this-&gt;eavConfig = $eavConfig;
    $this-&gt;logger = $logger;
    $this-&gt;attributeResource = $attributeResource;
    $this-&gt;moduleDataSetup = $moduleDataSetup;
}

public function apply()
{
    $this-&gt;moduleDataSetup-&gt;getConnection()-&gt;startSetup();
    $this-&gt;addPostcodeAttribute();
    $this-&gt;moduleDataSetup-&gt;getConnection()-&gt;endSetup();
}

public function addPostcodeAttribute()
{
    $eavSetup = $this-&gt;eavSetupFactory-&gt;create();
    $eavSetup-&gt;addAttribute(
            \Magento\Customer\Model\Customer::ENTITY,
            'title',
            [
                'type'          =&gt; 'varchar',
                'label'         =&gt; 'Postcode',
                'input'         =&gt; 'text',
                'required'      =&gt; 1,
                'visible'       =&gt; 1,
                'user_defined'  =&gt; 1,
                'sort_order'    =&gt; 999,
                'position'      =&gt; 999,
                'system'        =&gt; 0
            ]
    );

    $attributeSetId = $eavSetup-&gt;getDefaultAttributeSetId(\Magento\Customer\Model\Customer::ENTITY);
    $attributeGroupId = $eavSetup-&gt;getDefaultAttributeGroupId(\Magento\Customer\Model\Customer::ENTITY);

    $attribute = $this-&gt;eavConfig-&gt;getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'customer_postcode');
    $attribute-&gt;setData('attribute_set_id', $attributeSetId);
    $attribute-&gt;setData('attribute_group_id', $attributeGroupId);
    $attribute-&gt;setData('used_in_forms', [ 'adminhtml_customer', 'customer_account_create' ]);
    $this-&gt;attributeResource-&gt;save($attribute);
}

public static function getDependencies()
{
    return [];
}

public function revert()
{
    $this-&gt;moduleDataSetup-&gt;getConnection()-&gt;startSetup();
    $eavSetup = $this-&gt;eavSetupFactory-&gt;create(['setup' =&gt; $this-&gt;moduleDataSetup]);
    $eavSetup-&gt;removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'customer');
    $this-&gt;moduleDataSetup-&gt;getConnection()-&gt;endSetup();
}

public function getAliases()
{
    return [];
}

}

I removed the module using the following command:

bin/magento module:disable JR2_ExtraCustomerData --clear-static-content
bin/magento setup:upgrade

But the module still remains because I am getting an error when accessing Admin -> Customers:

1 exception(s):
Exception #0 (Magento\Framework\Exception\LocalizedException): The "formElement" configuration parameter is required for the "postcode" field.

It seems the module still exists in the database. I had this problem before and I had to reinstall the entire development server because I couldn't figure out how to remove the module.

Any ideas?

BugHunterUK
  • 82
  • 13

2 Answers2

0
  1. Try delete the entire folder "ExtraCustomerData" (keep an backup also before deleting)
  2. Then open the database in sqlyog or any other editor then drop all the related tables to this module manually.
Vibhore Jain
  • 679
  • 1
  • 6
  • 16
  • Do you know what tables are related? I removed the rows in the following, but the problem persists: customer_eav_attribute, customer_form_attribute, customer_eav_attribute_website, and entity_attribute_id – BugHunterUK May 04 '21 at 14:36
  • Only those tables which are involved with ExtraCustomerData, those table you need to drop. Deleting only rows is not enough. – Vibhore Jain May 04 '21 at 14:39
  • I don't actually create any tables. I am creating an EAV attribute for the customer which is stored as an EAV attribute in the tables above. – BugHunterUK May 04 '21 at 14:40
  • Check this link - https://magento.stackexchange.com/questions/144620/magento2-how-to-remove-created-eav-attribute-for-which-is-added-through-install – Vibhore Jain May 04 '21 at 14:46
0

If you are using the Phpmyadmin data manager you can go there and search for the "eav_attribute" table, then delete your custom attribute from the database.