cancel
Showing results for 
Search instead for 
Did you mean: 

Admin Customer Observer

Admin Customer Observer

Hi,

I'v added a field to admin customer form in Vendor\Module\view\adminhtml\ui_component\customer_form.xml with 

 <fieldset name="customer">
        <field name="confirmed">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">Magento\Config\Model\Config\Source\Yesno</item>
                <item name="config" xsi:type="array">
                    <item name="source" xsi:type="string">customer</item>
                    <item name="label" translate="true" xsi:type="string">Confirmed</item>
                    <item name="sortOrder" xsi:type="string">210</item>
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="dataType" xsi:type="string">select</item>
                    <item name="user_defined" xsi:type="boolean">true</item>
                </item>
            </argument>
        </field>
...
And it's showing perfectly. So, now i'm trying to load the correct value, if it's confirmed or not to be able to change it.


I'm using customer_load_after (please advice if any other would be better suitable) to try to set the confirmed field. As a test I also tried to change the firstname.
In Vendor\Module\etc\adminhtml\events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="customer_load_after">
        <observer name="customerLoad" instance="Vendor\Module\Observer\Backend\CustomerLoadObserver" />
    </event>
</config>

And in the observer execute:

public function execute(\Magento\Framework\Event\Observer $observer)
    {
        if (!$this->helper->isEnablePasswordSection()) {
            return;
        }

        $this->customer = $observer->getData('customer');
        if(!$this->customer) {
            return;
        }

        $this->customer->setConfirmed(0);
        $this->customer->setFirstname('test');
        
        return $this;
    }

The name in title is correctly changed to 'test' and lastname, but the firstname in the form is still the original from db.

How an achieved this?

 

I also tried the customer_load_before, but don't know how to add to the select query there.