如何在Magento2的管理产品编辑页面的页面操作按钮旁边添加一个新按钮


How to add a new button next to the page action buttons on the admin product edit page of Magento2

我想在Magento 2的管理产品编辑页面的页面操作按钮(返回,保存)旁边添加一个新按钮。这个新按钮将执行自定义操作。任何人都可以让我知道,我如何在管理产品编辑页面的页面操作按钮区域创建新的按钮。我使用的是Magento 2.0.2版本

经过一番研究,我发现,我们可以通过编辑核心文件vendor'magento'module-catalog'Block'Adminhtml' product ' edit .php中的_prepareLayout()函数来在管理产品编辑页面中创建一个自定义按钮

为了做到这一点,我在我的模块中扩展了这个块文件。为此,首先我编辑了我的模块(MyVendor/MyModule/etc/di.xml)中的di.xml文件

<preference for="Magento'Catalog'Block'Adminhtml'Product'Edit" type="MyVendor'MyModule'Block'Adminhtml'Product'Edit" />

然后我扩展了Block文件到我的模块(MyVendor/MyModule/Block/Adminhtml/Product/Edit.php)

名称空间块MyVendor ' MyModule里' ' Adminhtml '产品;

类编辑扩展'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();
}

}

这会在Admin产品编辑页面中创建自定义按钮。