DEV Community

Anuradha Fernando
Anuradha Fernando

Posted on • Updated on

How to pass a parameter from one controller to another in Magento2 Admin form

Do you want to pass a parameter when clicking the save button in the admin form? It's not just like a hidden value to append to the form URL.

http://www.m235p2.com/admin/module/module/new/order_id/1/key/db2ee099a204cb0d706bf711e49830ef693cb0c763baf306

We are in the edit form and we have "order_id" with us, we are going to pass this order_id to "save controller" when clicking the Save button.

Add GenericButton.php (if not exists).
Vendor/Module/Block/Adminhtml/Module/Edit/GenericButton.php

<?php
/**
 * Copyright © Nose All rights reserved.
 * See LICENSE.txt for license details.
 */
namespace Vendor\Module\Block\Adminhtml\Module\Edit;

use Magento\Backend\Block\Widget\Context;
use Magento\Cms\Api\BlockRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Vendor\Module\Api\Data\ModuleInterface;

abstract class GenericButton
{
    /**
     * @var Context
     */
    protected $context;

    /**
     * @var BlockRepositoryInterface
     */
    protected $blockRepository;

    /**
     * GenericButton constructor.
     * @param Context $context
     */
    public function __construct(
        Context $context,
        BlockRepositoryInterface $blockRepository
    ) {
        $this->context = $context;
        $this->blockRepository = $blockRepository;
    }    

    /**
     * Generate url by route and parameters
     *
     * @param   string $route
     * @param   array $params
     * @return  string
     */
    public function getUrl($route = '', $params = [])
    {
        return $this->context->getUrlBuilder()->getUrl($route, $params);
    }

    /**
     * Get order id from the URL
     *
     * @return mixed
     */
    public function getOrderId()
    {
        try {
            return $this->context->getRequest()->getParam('order_id');
        } catch (NoSuchEntityException $e) {
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

Add SaveButton.php (if not exists)
Vendor/Module/Block/Adminhtml/Module/Edit/SaveButton.php

<?php
/**
 * Copyright © Nose All rights reserved.
 * See LICENSE.txt for license details.
 */
namespace Vendor/Module/Block/Adminhtml/Module/Edit;

use Magento\Backend\Block\Widget\Context;
use Magento\Cms\Api\BlockRepositoryInterface;
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

class SaveButton extends GenericButton implements ButtonProviderInterface
{
    /**
     * Url Builder
     *
     * @var \Magento\Framework\UrlInterface
     */
    protected $urlBuilder;

    /**
     * CustomButton constructor.
     *
     * @param \Magento\Backend\Block\Widget\Context $context
     */
    public function __construct(Context $context, BlockRepositoryInterface $blockRepository)
    {
        $this->urlBuilder = $context->getUrlBuilder();
        parent::__construct($context, $blockRepository);
    }

    /**
     * Set order_id to save button as a parameter
     *  
     * @return array
     */
    public function getButtonData()
    {
        $data = [
            'label' => __('Save'),
            'class' => 'save',
            'on_click' => '',
            'sort_order' => 20,
            'data_attribute' => [
                'mage-init' => [
                    'Magento_Ui/js/form/button-adapter' => [
                        'actions' => [
                            [
                                'targetName' => 'vendor_module_module_form.vendor_module_module_form',
                                'actionName' => 'save',
                                'params' => [
                                    true,
                                    ['order_id' => $this->getOrderId()],
                                ]
                            ]
                        ]
                    ]
                ],

            ]
        ];

        return $data;
    }
}

Enter fullscreen mode Exit fullscreen mode

In Save.php
Vendor/Module/Controller/Adminhtml/Module/Save.php

/**
* Save action
*
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
    /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
    $resultRedirect = $this->resultRedirectFactory->create();
    $data = $this->getRequest()->getParams();
    $orderId = $this->getRequest()->getParam('order_id');

    //continue with the rest of the code
Enter fullscreen mode Exit fullscreen mode

cheers!!

Top comments (0)