0

How can I hide the customer custom attribute if the value is null?

Ultimately I want to show this field on edit if it is not null and I don't want to show this field when we create a new customer

1 Answer 1

0

Create a Plugin for Customer Form Block:

Magento\Customer\Block\Adminhtml\Edit\Form

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

Create the Plugin Class

namespace YourVendor\YourModule\Plugin\Adminhtml\Customer;

use Magento\Customer\Block\Adminhtml\Edit\Form as CustomerEditForm;

class Form
{
    public function afterGetFormHtml(CustomerEditForm $subject, $result)
    {
        // Replace 'custom_attribute_code' with your custom attribute's code
        if ($subject->getCustomer()->getCustomAttribute('custom_attribute_code')->getValue() === null) {
            $result = str_replace('<div class="admin__field', '<div style="display:none" class="admin__field', $result);
        }
        return $result;
    }
}

After that clear cache.

php bin/magento cache:clean
php bin/magento cache:flush
php bin/magento indexer:reindex

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.