0

I want to add multiple emails into customers account information as per below screen-shot.

enter image description here

Any help would be appreciated.

1

Add dynamic custom field in customer edit/create form

enter image description here

Create file : [Namespace]/[Module]/view/base/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">      
            <dynamicRows name="multiple_emails">
                <settings>
                    <addButtonLabel translate="true">Add More</addButtonLabel>
                    <additionalClasses>
                        <class name="admin__field-wide">true</class>
                    </additionalClasses>
                    <componentType>dynamicRows</componentType>
                </settings>
                <container name="record" component="Magento_Ui/js/dynamic-rows/record">
                    <argument name="data" xsi:type="array">
                        <item name="config" xsi:type="array">
                            <item name="isTemplate" xsi:type="boolean">true</item>
                            <item name="is_collection" xsi:type="boolean">true</item>
                            <item name="componentType" xsi:type="string">container</item>
                            <item name="positionProvider" xsi:type="string">attribute_options.position</item>
                        </item>
                    </argument>
                    <field name="emja_more_emails" sortOrder="10" 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>
                            <label translate="true">Email</label>
                            <dataType>text</dataType>
                            <dataScope>emails</dataScope>
                        </settings>
                    </field>
                    <field name="apply_to" formElement="multiselect"  sortOrder="20">
                        <settings>
                            <dataType>text</dataType>
                            <label translate="true">Apply To</label>
                            <disabled>false</disabled>
                            <dataScope>apply_to</dataScope>
                        </settings>
                         <formElements>
                            <multiselect>
                                <settings>
                                    <options class="[Namespace]\[Module]\Model\EmailTemplates"/>
                                </settings>
                            </multiselect>
                        </formElements>
                    </field>
                    <actionDelete  sortOrder="30">
                    <argument name="data" xsi:type="array">
                        <item name="config" xsi:type="array">
                            <item name="componentType" xsi:type="string">actionDelete</item>
                            <item name="dataType" xsi:type="string">text</item>
                            <item name="fit" xsi:type="boolean">false</item>
                            <item name="label" xsi:type="string">Actions</item>
                            <item name="sortOrder" xsi:type="string">500</item>
                            <item name="additionalClasses" xsi:type="string">data-grid-actions-cell</item>
                            <item name="template" xsi:type="string">Magento_Backend/dynamic-rows/cells/action-delete</item>
                        </item>
                    </argument>
                </actionDelete>
                </container>        
            </dynamicRows>

    </fieldset>        
    </form>

Create di file for value selected in dynamic rows

[Namespace]/[Module]/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\DataProvider">
        <plugin name="add-size-grouped-products" type="[Namespace]\[Module]\Model\Customer\DataProvider" sortOrder="5" />
    </type>
</config>

[Namespace]/[Module]/Model/Customer/DataProvider.php

<?php
namespace [Namespace]\[Module]\Model\Customer;

class DataProvider 
{
    public function afterGetData(\Magento\Customer\Model\Customer\DataProvider $subject, $result)
    {

    if($result){
        $customer_id = key($result);
        $customer_data =  $result[$customer_id]['customer'];        

        if($customer_data){
            if(array_key_exists('emja_more_emails',$customer_data)){
                $emja_more_emails = unserialize($customer_data['emja_more_emails']);
                if(count($emja_more_emails) > 0){
                    foreach ($emja_more_emails as  $value) {
                        //$logger->info($value);    
                        $result[$customer_id]['customer']['multiple_emails']['multiple_emails'][] = $value;
                    }
                }
            }
        }


    }

        return $result;
    }

}

Create event for save data to customer

[Namespace]/[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_save_before">
        <observer name="multiple_emails_save" instance="[Namespace]\[Module]\Observer\AddMultipleEmails" />
    </event>    
</config>

[Namespace]/[Module]/Observer/AddMultipleEmails.php

<?php

namespace [Namespace]\[Module]\Observer;

class AddMultipleEmails implements \Magento\Framework\Event\ObserverInterface
{

     protected $_request;

    public function __construct(
        \Magento\Framework\App\RequestInterface $request
    ) {
        $this->_request = $request;        
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $customer = $observer->getCustomer();
        $data = $this->_request->getParams();

        if($data){
            $customerData = $data['customer'];
            $serialize_emails = '';
            if(array_key_exists('multiple_emails',$customerData)){
                $serialize_emails =  serialize($data['customer']['multiple_emails']['multiple_emails']);            

            }
            $customer->setEmjaMoreEmails($serialize_emails);
        }

    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.