What is the proper way to pass mass action selected IDs to a UI Component form in Magento 2?

424 Views Asked by At

I am working on a section , where the admin can select multiple customers from customer grid, and then select an custom option from massAction drop down.

enter image description here

Which will open an ui form.

enter image description here

On clicking on send, I want to send message to the selected customers.

I am currently setting the selected customer ids in session at the mass action controller, and getting them in the form submit controller. But is there any proper way to do this ? Without using session ?

Following is what I have tried

Vendor/Module/view/adminhtml/ui_component/customer_listing.xml

<listingToolbar name="listing_top">
    <massaction name="listing_massaction">
        <action name="sms_notification">
            <settings>
                <url path="daplcore/notification/view"/>
                <type>sms_notification</type>
                <label translate="true">Send Notification/Sms</label>
            </settings>
        </action>
    </massaction>
</listingToolbar>

Vendor/Module/Controller/Adminhtml/Notification/View.php `

public function execute()
{
    $selectedIds = $this->getRequest()->getParam('selected');
    $this->adminSession->setSelectedCustomers($selectedIds);

    $resultPage = $this->resultPageFactory->create();
    $resultPage->getConfig()->getTitle()->prepend((__('Notification Sender')));

    return $resultPage;
}

Vendor/Module/view/adminhtml/layout/daplcore_notification_view.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="styles"/>
    <body>
        <referenceContainer name="content">
            <uiComponent name="daplcore_notification_view_ui"/>
        </referenceContainer>
    </body>
</page>

Vendor/Module/view/adminhtml/ui_component/daplcore_notification_view_ui.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">
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">daplcore_notification_view_ui.daplcore_notification_view_ui_data_source</item>
        </item>
        <item name="label" xsi:type="string" translate="true">Notification Form</item>
        <item name="template" xsi:type="string">templates/form/collapsible</item>
    </argument>
    <settings>
        <buttons>
            <button name="save" class="Vendor\Module\Block\Adminhtml\Notification\Send"/>
        </buttons>
        <namespace>daplcore_notification_view_ui</namespace>
        <dataScope>data</dataScope>
        <deps>
            <dep>daplcore_notification_view_ui.daplcore_notification_view_ui_data_source</dep>
        </deps>
    </settings>
    <dataSource name="daplcore_notification_view_ui_data_source">
        <argument name="data" xsi:type="array">
            <item name="js_config" xsi:type="array">
                <item name="component" xsi:type="string">Magento_Ui/js/form/provider</item>
            </item>
        </argument>
        <settings>
            <submitUrl path="daplcore/notification/send"/>
        </settings>
        <dataProvider class="Vendor\Module\Model\Ui\NotificationDataProvider" name="daplcore_notification_view_ui_data_source">
            <settings>
                <requestFieldName>id</requestFieldName>
                <primaryFieldName>id</primaryFieldName>
            </settings>
        </dataProvider>
    </dataSource>
    <fieldset name="general">
        <settings>
            <label/>
        </settings>
        <field name="type" sortOrder="10" formElement="select">
            <settings>
                <dataType>text</dataType>
                <label translate="true">Notification Type</label>
                <dataScope>type</dataScope>
                <switcherConfig>
                    <rules>
                        <rule name="0">
                            <value>sms</value>
                            <actions>
                                <action name="0">
                                    <target>daplcore_notification_view_ui.daplcore_notification_view_ui.general.title</target>
                                    <callback>hide</callback>
                                </action>
                            </actions>
                        </rule>
                        <rule name="1">
                            <value>push</value>
                            <actions>
                                <action name="0">
                                    <target>daplcore_notification_view_ui.daplcore_notification_view_ui.general.title</target>
                                    <callback>show</callback>
                                </action>
                            </actions>
                        </rule>
                    </rules>
                    <enabled>true</enabled>
                </switcherConfig>
            </settings>
            <formElements>
                <select>
                    <settings>
                        <options class="Vendor\Module\Model\Source\NotificationOptions" />
                        <caption translate="true">-- Please Select --</caption>
                    </settings>
                </select>
            </formElements>
        </field>
        <field name="title" sortOrder="20" formElement="input">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="source" xsi:type="string">block</item>
                </item>
            </argument>
            <settings>
                <validation>
                    <rule name="required-entry" xsi:type="boolean">true</rule>
                </validation>
                <dataType>text</dataType>
                <label translate="true">Title</label>
                <dataScope>title</dataScope>
            </settings>
        </field>
        <field name="body" sortOrder="30" formElement="input">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="source" xsi:type="string">block</item>
                </item>
            </argument>
            <settings>
                <validation>
                    <rule name="required-entry" xsi:type="boolean">true</rule>
                </validation>
                <dataType>textarea</dataType>
                <label translate="true">Body</label>
                <dataScope>body</dataScope>
            </settings>
        </field>
    </fieldset>
</form>
0

There are 0 best solutions below