Magento 2: Add new button on Category Edit Page in Admin Panel
For this example we will assume you have a custom module. Let's call it Namespace_Module.
Now, create files as below:
Namespace_Module\view\adminhtml\ui_component\category_form.xml
Namespace_Module\Block\Adminhtml\Category\Edit\Button\AssignButton.php
Namespace_Module\Controller\Adminhtml\Category\Assign.php
Voila !! That's it. Now, clear all caches and check the category page in admin panel. There will be a new button called "Assign".
Now, create files as below:
Namespace_Module\view\adminhtml\ui_component\category_form.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* @author Reena Parekh <reena1.parekh@gmail.com>
*/
-->
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<argument name="data" xsi:type="array">
<item name="buttons" xsi:type="array">
<item name="button-unique-identifier-here" xsi:type="string">
Namespace\Module\Block\Adminhtml\Category\Edit\Button\AssignButton
</item>
</item>
</argument>
</form>
Namespace_Module\Block\Adminhtml\Category\Edit\Button\AssignButton.php
Namespace_Module\Controller\Adminhtml\Category\Assign.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php
/**
* @author Reena Parekh <reena1.parekh@gmail.com>
*/
namespace Namespace\Module\Controller\Adminhtml\Category;
/**
* Class Assign
*/
class Assign extends \Magento\Catalog\Controller\Adminhtml\Category
{
/**
* @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|void
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
if ($categoryId = $this->getRequest()->getParam('category_id')) {
//Add code related to whatever you want to do on button click here.
$this->messageManager->addSuccess(__('Operation performed successfully.'));
$resultRedirect->setPath('catalog/category/edit', ['id' => $categoryId, '_current' => true]);
}
return $resultRedirect;
}
}
Voila !! That's it. Now, clear all caches and check the category page in admin panel. There will be a new button called "Assign".
Comments
Post a Comment