1

I want to move fields on customer/index/edit. Creating another fieldset can be done with

<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="my-new-fields">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">Additional Information</item>
        </item>
    </argument>
    <field name="blablupp">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" translate="true" xsi:type="string">buueeyyy</item>
                <item name="visible" xsi:type="boolean">true</item>
                <item name="dataType" xsi:type="string">text</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="source" xsi:type="string">customer</item>
            </item>
        </argument>
    </field>
</fieldset>

And even hiding a field from an existing fieldset is easy by adding to the customer fieldset:

<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="customer">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">Additional Information</item>
        </item>
    </argument>
    <field name="prefix">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="visible" xsi:type="boolean">false</item>
        </argument>
    </field>
</fieldset>

But what I really want is to move a field from customer to another to simplify life for unexperienced admin users. How is that accomplished?

2
  • tell me what field you want to move and where? Commented Feb 18, 2017 at 9:59
  • I want to move fields like dob, gender, suffix to the custom fieldset I created (name="my-new-fields") Commented Feb 18, 2017 at 16:08

1 Answer 1

2

I am going to answer my own question:

I want to have a fieldset "company" in an separate tab with field "company_name" in customer edit form. The eav attribute is saved in customer entity.

Create the file Vendor\Module\view\adminhtml\ui_component\customer_form.xml

<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="customer">
        <fieldset name="company">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">My custom fieldset</item>
                </item>
            </argument>
            <field name="company_name">
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="label" xsi:type="string">Firma</item>
                        <item name="visible" xsi:type="boolean">true</item>
                        <item name="dataScope" xsi:type="string">company_name</item>
                        <item name="dataType" xsi:type="string">varchar</item>
                        <item name="formElement" xsi:type="string">input</item>
                        <item name="sortOrder" xsi:type="number">5</item>
                        <item name="imports" xsi:type="array">
                            <item name="default" xsi:type="string">${ $.provider }:data.customer.company_name</item>
                        </item>
                    </item>
                </argument>
            </field>
        </fieldset>
    </fieldset>
</form>

Note the item. That imports the data form customer fieldset into my custom fieldset. But that only reads data. To alo write it back to the customer entity I needed to create an observer:

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="adminhtml_customer_prepare_save">
        <observer instance="Vendor\Module\Observer\Customer\AdminhtmlCustomerSaveBefore" name="vendor_module_observer_adminhtml_customer_save_before"/>
    </event>
</config>

Observer class:

<?php

namespace Vendor\Module\Observer\Customer;

use Magento\Customer\Model\Customer; 
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class AdminhtmlCustomerSaveBefore implements ObserverInterface
{
    public function execute(
        Observer $observer
    )
    {
        /** @var  Customer $customer */
        $customer = $observer->getEvent()->getCustomer();
        $params = $observer->getEvent()->getRequest()->getParams();
        if (isset($params['company']['company_name'])) {
            $customer->setCustomAttribute('company_name', $params['company']['company_name']);

    }
}

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.