Magento – Magento 2 Admin Form add parameter to form submit url

adminhtmldata-providerformsmagento-2.1uicomponent

I need to add a parameter from the registry to my form save action

Currently i'm attempting to do this by storing my parameter in the registry, then collecting it in the the buttons save block class. This works in as far as it changes the on-click event of the button, but does not affect the submit url of the form. Therefore when it comes to retrieving the order_id in the save controller the parameter is not there.

<?php

namespace Blah\OrderResources\Block\Adminhtml\Buttons;

use Blah\OrderResources\Block\Adminhtml\Buttons\Generic;
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;


class Save extends Generic implements ButtonProviderInterface
{
    /**
     * get button data
     *
     * @return array
     */
    public function getButtonData()
    {
        $orderId = $this->getOrderId();
        // Add the order id to the url so we can maintain the association
        $url = $this->getUrl('order-resource/form/save', ['order_id' => $orderId]);

        return [
            'label' => __('Save Resource'),
            'class' => 'save primary',
            'data_attribute' => [
                'mage-init' => ['button' => ['event' => 'save']],
                'form-role' => 'save',
            ],
            'sort_order' => 90,
            'url' => $url
        ];

    }



}

I imagine that this needs to be done using a data provider but I can't seem to be able to figure this out. Here is the ui-component xml where the submit url is set :

<dataSource name="order_resources_form_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">Blah\OrderResources\Model\DataProvider</argument>
            <argument name="name" xsi:type="string">order_resources_form_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">resource_id</argument>
            <argument name="requestFieldName" xsi:type="string">resource_id</argument>
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="submit_url" xsi:type="url" path="order_resources/form/save"/>
                </item>
            </argument>
        </argument>
        <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>
    </dataSource>

Does anyone know how I can achieve this ?

Best Answer

You can add a URL parameter to the submit URL more easily using a button adapter as follows:

 public function getButtonData()
    {
        return
            [
                'label' => __('Save'),
                'class' => 'save',
                'on_click' => '',
                'sort_order' => 80,
                'data_attribute' => [
                    'mage-init' => [
                        'Magento_Ui/js/form/button-adapter' => [
                            'actions' => [
                                [
                                    'targetName' => 'your_ui_component_name.your_ui_component_name',
                                    'actionName' => 'save',
                                    'params' => [
                                        true,
                                        ['order_id' => $this->getOrderId()],
                                    ]
                                ]
                            ]
                        ]
                    ],

                ]
            ];
    }
Related Topic