This is one of the best solutions I have seen so far without using plugins
MagePal/CustomButton/view/adminhtml/layout/adminhtml_sales_order_view.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="sales_order_edit">
<block class="MagePal\CustomButton\Block\Adminhtml\Order\View\Buttons" name="custom_buttons">
<action method="addButtons"/>
</block>
</referenceBlock>
</body>
</page>
MagePal/CustomButton/Block/Adminhtml/Order/View/Buttons.php
namespace MagePal\CustomButton\Block\Adminhtml\Order\View;
class Buttons extends \Magento\Sales\Block\Adminhtml\Order\View
{
public function __construct(
\Magento\Backend\Block\Widget\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Sales\Model\Config $salesConfig,
\Magento\Sales\Helper\Reorder $reorderHelper,
array $data = []
) {
parent::__construct($context, $registry, $salesConfig, $reorderHelper, $data);
}
public function addButtons()
{
$parentBlock = $this->getParentBlock();
$buttonUrl = $this->_urlBuilder->getUrl(
'adminhtml/custombutton/new',
['order_id' => $parentBlock->getOrderId()]
);
$this->getToolbar()->addChild(
'create_custom_button',
\Magento\Backend\Block\Widget\Button::class,
['label' => __('Custom Button'), 'onclick' => 'setLocation(\'' . $buttonUrl . '\')']
);
}
return $this;
}
}