You can do this by plugin.
Create this class:
<?php
namespace NameSpace\ModuleName\Plugin\Sales\Block\Adminhtml\Order;
use Magento\Sales\Block\Adminhtml\Order\View as OrderView;
use Magento\Framework\UrlInterface;
use Magento\Framework\AuthorizationInterface;
class View
{
/** @var \Magento\Framework\UrlInterface */
protected $_urlBuilder;
/** @var \Magento\Framework\AuthorizationInterface */
protected $_authorization;
public function __construct(
UrlInterface $url,
AuthorizationInterface $authorization
)
{
$this->_urlBuilder = $url;
$this->_authorization = $authorization;
}
public function beforeSetLayout(OrderView $view)
{
$url = $this->_urlBuilder->getUrl('your_route', ['order_id' => $view->getOrderId()]);
$view->updateButton('back', 'onclick', 'setLocation(\'' . $url . '\')');
}
}
And in your 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\Sales\Block\Adminhtml\Order\View">
<plugin name="your_module_adminhtml_order_view_add_button" type="NameSpace\ModuleName\Plugin\Sales\Block\Adminhtml\Order\View" sortOrder="10" />
</type>
</config>
This should work.
UPDATE
You could do this:
public function beforeSetLayout(OrderView $view) {
$param = $view->getRequest()->getParam('redirect_back'); //you can even pass your route and use it in codntion then build the back url accordingly.
if ( $param == '1') {
$url = $this->_urlBuilder->getUrl('your_route', ['order_id' => $view->getOrderId()]);
$view->updateButton('back', 'onclick', 'setLocation(\'' . $url . '\')');
}
Now, in your custom grid build URL with the redirect_back/1
. So your full grid URL should be like this:
http://example.com/admin/sales/order/view/order_id/20/redirect_back/1/key/1c8e5cae74bb5d37038fbd3b79e4637caf6fd17112e40b51ef1bad63aef280b9/