I have created one custom module and through that module, I want to add a custom button in Admin product edit form.
I have created a plugin using the below reference
How to Add a Custom Button to Admin Sales Order View in Magento2
di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="\Magento\Catalog\Block\Adminhtml\Product\Edit">
<plugin name="Company_Module::pluginBeforeView" type="Company\Module\Plugin\PluginBeforeView" />
</type>
</config>
PluginBeforeView.php
namespace Company\Module\Plugin;
class PluginBeforeView
{
public function beforeGetOrderId(\Magento\Catalog\Block\Adminhtml\Product\Edit $subject){
$subject->addButton(
'mybutton',
['label' => __('My Buttion'), 'onclick' => 'setLocation(window.location.href)', 'class' => 'reset'],
-1
);
return null;
}
}
But it did not work. Any idea how to solve this.
Best Answer
The product form is generated via ui-components.
The ui component name for product form is
view/adminhtml/ui_component/product_form.xml
.You need to create a file with the same name and path in your own module with the following content:
Then create the class
[Namespace]\[Module]\Block\Adminhtml\Product\Edit\Button\CustomButton
in the file[Namespace]/[Module]/Block/Adminhtml/Product/Edit/Button/CustomButton.php
Your ui component file should be merged with the main file and your buttons should appear among the other buttons.