The Magento\Customer\Block\Adminhtml\Edit
class is deprecated and no longer used for the customer edit/create page in the admin.
To remove a field in the customer create and edit page in the admin, you'll need to override the field declared in vendor/magento/module-customer/view/base/ui_component/customer_form.xml
file.
In your case, modify the page in the admin area, you'll need to put your file in the adminhtml
directory.
To remove the Name Prefix, create <your module dir>/view/adminhtml/ui_component/customer_form.xml
file with the following content.
Example: app/code/TuVan/Customer/view/adminhtml/ui_component/customer_form.xml
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="customer">
<field name="prefix" formElement="input">
<settings>
<visible>false</visible>
</settings>
</field>
</fieldset>
</form>
To remove a field in the customer create and edit page in the admin based on a condition in PHP, you need to modify the getMeta()
method in the DataProvider class of the UI form component.
The DataProvider of the Customer form is declared in vendor/magento/module-customer/view/base/ui_component/customer_form.xml
file:
<dataProvider class="Magento\Customer\Model\Customer\DataProviderWithDefaultAddresses" name="customer_form_data_source">
Implementation:
- Create a
di.xml
in the admin area: <your module dir>/etc/adminhtml/di.xml
.
Example: app/code/TuVan/Customer/etc/adminhtml/di.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Customer\Model\Customer\DataProviderWithDefaultAddresses">
<plugin name="manage_prefix_field_visibility" type="TuVan\Customer\Plugin\ManagePrefixFieldVisibility"/>
</type>
</config>
- Create plugin class:
<your module dir>/Plugin/ManagePrefixFieldVisibility.php
.
Example: app/code/TuVan/Customer/Plugin/ManagePrefixFieldVisibility.php
<?php
declare(strict_types=1);
namespace TuVan\Customer\Plugin;
use Magento\Customer\Model\Customer\DataProviderWithDefaultAddresses;
/**
* Plugin for managing prefix field in Customer form Data Provider in the admin area.
*/
class ManagePrefixFieldVisibility
{
public function afterGetMeta(
DataProviderWithDefaultAddresses $subject,
array $result
): array {
$isDisabledPrefixField = false;
if (1==1) {//Remember to change this condition
$isDisabledPrefixField = true;
}
$config = [
'customer' => [
'children' => [
'prefix' => [
'arguments' => [
'data' => [
'config' => [
'componentDisabled' => $isDisabledPrefixField
],
],
],
]
],
],
];
return array_replace_recursive($result, $config);
}
}