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->eavSetupFactory = $eavSetupFactory;
$this->eavConfig = $eavConfig;
$this->logger = $logger;
$this->attributeResource = $attributeResource;
$this->moduleDataSetup = $moduleDataSetup;
}
public function apply()
{
$this->moduleDataSetup->getConnection()->startSetup();
$this->addPostcodeAttribute();
$this->moduleDataSetup->getConnection()->endSetup();
}
public function addPostcodeAttribute()
{
$eavSetup = $this->eavSetupFactory->create();
$eavSetup->addAttribute(
\Magento\Customer\Model\Customer::ENTITY,
'title',
[
'type' => 'varchar',
'label' => 'Postcode',
'input' => 'text',
'required' => 1,
'visible' => 1,
'user_defined' => 1,
'sort_order' => 999,
'position' => 999,
'system' => 0
]
);
$attributeSetId = $eavSetup->getDefaultAttributeSetId(\Magento\Customer\Model\Customer::ENTITY);
$attributeGroupId = $eavSetup->getDefaultAttributeGroupId(\Magento\Customer\Model\Customer::ENTITY);
$attribute = $this->eavConfig->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'customer_postcode');
$attribute->setData('attribute_set_id', $attributeSetId);
$attribute->setData('attribute_group_id', $attributeGroupId);
$attribute->setData('used_in_forms', [ 'adminhtml_customer', 'customer_account_create' ]);
$this->attributeResource->save($attribute);
}
public static function getDependencies()
{
return [];
}
public function revert()
{
$this->moduleDataSetup->getConnection()->startSetup();
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
$eavSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'customer');
$this->moduleDataSetup->getConnection()->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?
customer_eav_attribute,customer_form_attribute,customer_eav_attribute_website, andentity_attribute_id– BugHunterUK May 04 '21 at 14:36