I want to add a new button next to the page action buttons (Back, Save) on the admin product edit page of Magento 2. This new button will perform a custom action. Can anyone please let me know, how can I create new button in the page-actions buttons area of the admin product edit page. I am using Magento 2.0.2 version.
After doing some research, I have found out that, we can create a custom button in the admin product edit page by editing the _prepareLayout() function inside the core file, vendor\magento\module-catalog\Block\Adminhtml\Product\Edit.php
In order to do this, I have extended this block file in my module. For that, first I have edited the di.xml file in my module (MyVendor/MyModule/etc/di.xml)
<preference for="Magento\Catalog\Block\Adminhtml\Product\Edit" type="MyVendor\MyModule\Block\Adminhtml\Product\Edit" />
Then I have extended the Block file to my module (MyVendor/MyModule/Block/Adminhtml/Product/Edit.php)
namespace MyVendor\MyModule\Block\Adminhtml\Product;
class Edit extends \Magento\Catalog\Block\Adminhtml\Product\Edit {
/**
* Add elements in layout
*
* @return $this
*/
protected function _prepareLayout()
{
$this->getToolbar()->addChild(
'custom_buttonfor_myaction',
'Magento\Backend\Block\Widget\Button',
[
'label' => __('Custom Button'),
'title' => __('Custom Button'),
'onclick' => 'setLocation(\'' . $this->getUrl(
'mycustompath/*/',
['store' => $this->getRequest()->getParam('store', 0)]
) . '\')',
'class' => 'action-default primary'
]
);
return parent::_prepareLayout();
}
}
This Creates the custom button in the Admin product edit page.