0

In backend customer, when i want create new customer in customer/index/new, i want remove field Name Prefix

enter image description here

in file di.xml i have

<type name="Magento\Customer\Block\Adminhtml\Edit">
        <plugin name="disable_customer_field" type="YourVendor\YourModule\Plugin\Customer\Block\Adminhtml\EditPlugin" sortOrder="10" disabled="false"/>
</type>

and i have file php

<?php
namespace YourVendor\YourModule\Plugin\Customer\Block\Adminhtml;

class EditPlugin
{
    public function beforeSetForm(
        \Magento\Customer\Block\Adminhtml\Edit $subject,
        \Magento\Framework\Data\Form $form
    ) {
        $form->getElement('prefix')->setDisabled(true);
    }
}

But i not working, can someone help me please

2 Answers 2

0

Override the customer_form.xml file in your module.

Add following code in {Your Module Path}\view\base\ui_component\customer_form.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<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">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="source" xsi:type="string">customer</item>
                </item>
            </argument>
            <settings>
                <dataType>text</dataType>
                <visible>false</visible>
            </settings>
        </field>
    </fieldset>
</form>
1
  • Hi but can we have another way to check in php to i can check the session?
    – Tom
    Commented Sep 21, 2023 at 16:54
0

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:

  1. 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>
  1. 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);
    }
}

2
  • Hi but can we have another way to check in php to i can check the session?
    – Tom
    Commented Sep 21, 2023 at 16:54
  • @Boll I have added the solution for that.
    – Tu Van
    Commented Sep 22, 2023 at 4:17

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.