After trying many different ways, this is the only solution I could find that seem to work without affecting other modules. I would love to see other solutions.
Option 1
Create a plugin in Company/Module/etc/adminhtml/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Backend\Block\Widget\Button\Toolbar">
<plugin name="MagePal_TestBed::pluginBefore" type="MagePal\TestBed\Plugin\PluginBefore" />
</type>
</config>
Then in Plugin/PluginBefore.php
namespace MagePal\TestBed\Plugin;
class PluginBefore
{
public function beforePushButtons(
\Magento\Backend\Block\Widget\Button\Toolbar\Interceptor $subject,
\Magento\Framework\View\Element\AbstractBlock $context,
\Magento\Backend\Block\Widget\Button\ButtonList $buttonList
) {
$this->_request = $context->getRequest();
if($this->_request->getFullActionName() == 'sales_order_view'){
$buttonList->add(
'mybutton',
['label' => __('My Button'), 'onclick' => 'setLocation(window.location.href)', 'class' => 'reset'],
-1
);
}
}
}
Option 2
Create a plugin in Company/Module/etc/adminhtml/di.xml
<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="MagePal_TestBed::pluginBeforeView" type="MagePal\TestBed\Plugin\PluginBeforeView" />
</type>
</config>
Then in Plugin/PluginBeforeView.php
namespace MagePal\TestBed\Plugin;
class PluginBeforeView
{
public function beforeGetOrderId(\Magento\Sales\Block\Adminhtml\Order\View $subject){
$subject->addButton(
'mybutton',
['label' => __('My Buttion'), 'onclick' => 'setLocation(window.location.href)', 'class' => 'reset'],
-1
);
return null;
}
}
See Full Source code