Magento2 | PWA | GraphQL

Provide Category Product Edit Link at Admin for Magento 2


In this post we will check how to Add Category Product Edit Link at Admin for Magento 2.

You can find complete module on Github at Magelearn_CategoryProductEdit


Let's start it by creating custom module.

Create folder inside app/code/Magelearn/CategoryProductEdit

Add registration.php file in it:

<?php

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
    ComponentRegistrar::MODULE,
    'Magelearn_CategoryProductEdit',
    __DIR__
);

Add composer.json file in it:

{
    "name": "magelearn/module-category-product-edit",
    "description": "Provide Category Product Edit Link at Admin for Magento 2",
    "type": "magento2-module",
    "require": {},
    "authors": [
        {
            "name": "vijay rami",
            "email": "vijaymrami@gmail.com"
        }
    ],
    "license": "proprietary",
    "minimum-stability": "dev",
    "autoload": {
        "files": [
            "registration.php"
        ],
        "psr-4": {
            "Magelearn\\CategoryProductEdit\\": ""
        }
    }
}

Add etc/module.xml file in it:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Magelearn_CategoryProductEdit">
         <sequence>
            <module name="Magento_Catalog"/>
         </sequence>
    </module>
</config>
We will provide Category Product edit link at admin by calling Admin event/observer. (adminhtml_block_html_before)

Create file at etc/adminhtml/events.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="adminhtml_block_html_before">
        <observer name="admin_category_product_edit_link_html_block_html_before_observer"
        	instance="Magelearn\CategoryProductEdit\Observer\AdminhtmlBlockHtmlBeforeObserver" />
    </event>
</config>

Call the observer file at Observer/AdminhtmlBlockHtmlBeforeObserver.php

<?php

declare(strict_types=1);

namespace Magelearn\CategoryProductEdit\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Event\Observer;
use Magento\Catalog\Block\Adminhtml\Category\Tab\Product;

class AdminhtmlBlockHtmlBeforeObserver implements ObserverInterface
{
    /**
     * Request instance
     *
     * @var RequestInterface
     */
    protected $request;

    /**
     * @param RequestInterface $request
     */
    public function __construct(
        RequestInterface $request
    ) {
        $this->request = $request;
    }

    /**
     * Add edit product column
     *
     * @param Observer $observer
     * @return void
     * @throws \Exception
     */
    public function execute(Observer $observer)
    {
        $block = $observer->getEvent()->getBlock();
        if (false === ($block instanceof Product)) {
            return;
        }
        
        $block->addColumn(
            'action',
            [
                'header' => __('Action'),
                'type' => 'action',
                'getter' => 'getId',
                'actions' => [
                    [
                        'caption' => __('Edit'),
                        'url' => [
                            'base' => 'catalog/product/edit',
                            'params' => ['store' => $this->request->getParam('store')]

                        ],
                        'field' => 'id'
                    ]
                ],
                'filter' => false,
                'sortable' => false,
                'index' => 'stores',
                'header_css_class' => 'col-action',
                'column_css_class' => 'col-action'
            ]
        );
    }
}

0 Comments On "Provide Category Product Edit Link at Admin for Magento 2"

Back To Top