Magento – Magento 2 ; How to pass custom params with save button in UI Form

magento2ui-formuicomponent

I want to pass a value/params via the save button of my ui adminform.

i tried this:

public function getButtonData()
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $request = $objectManager->get('Magento\Framework\App\RequestInterface');
        $id = $request->getParam('id');

        return
            [
                'label' => __('Save'),
                'class' => 'save primary',
                'on_click' => '',
                'sort_order' => 80,
                'data_attribute' => [
                    'mage-init' => [
                        'Magento_Ui/js/form/button-adapter' => [
                            'actions' => [
                                [
                                    'targetName' => 'thechateau_magenest_bookable_save',
                                    'actionName' => 'save',
                                    'params' => [
                                        true,
                                        ['multidome_id' =>$id],
                                    ]
                                ]
                            ]
                        ]
                    ],

                ]
            ];
    }

I can indeed see the values in the save button of the phtml page.

<button id="save" title="Save" type="button" class="action- scalable save primary" data-mage-init="{&quot;Magento_Ui\/js\/form\/button-adapter&quot;:{&quot;actions&quot;:[{&quot;targetName&quot;:&quot;thechateau_magenest_bookable_save&quot;,&quot;actionName&quot;:&quot;save&quot;,&quot;params&quot;:[true,{&quot;multidome_id&quot;:&quot;1&quot;}]}]}}"  data-ui-id="save-button" >
    <span>Save</span>
</button>

however when i try to retreive these values it comes up null.

$request = $this->_objectManager->create('Magento\Framework\App\RequestInterface');


$multiDome = $request->getParam('multidome_id');

i also tried to add it as part of the URL of the save button but it does not even show up on this:

public function getSaveUrl()
    {
        return $this->getUrl('*/*/save',['param'=>'value']);

    }

Best Answer

In my case I used ButtonProviderInterface

namespace MyVendor\Logger\Block\Adminhtml\Log\Index;
use \Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

class ImportButton implements ButtonProviderInterface
{
    public function getButtonData()
    {
        return [
            'label' => __('Import Log'),
            'class' => 'action-secondary',
            'data_attribute' => [
                'mage-init' => [
                    'Magento_Ui/js/form/button-adapter' => [
                        'actions' => [
                            [
                                'targetName' => 'logger_log_listing.logger_log_listing.import_modal',
                                'actionName' => 'openModal'
                            ],
                        ]
                    ]
                ]
            ],
            'on_click' => '',
            'sort_order' => 20
        ];
    }
}

While declaring my button on the listing xml file logger_log_listing.xml, like so :

<listing 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">
...
</argument>
<settings>
        ...
        <buttons>
            <button name="add">
                ...
            </button>
            <button name="import" class="MyVendor\Logger\Block\Adminhtml\Log\Index\ImportButton"/>
        </buttons>
</settings>
<modal name="import_modal">
...
</modal>

PS: I don't know why but not using 'on_click' => '' resulted in redirection to base url (front page)

Related Topic