Magento: add button for own admin module action to admin products grid

Magento: add button for own admin module action to admin products grid

July 13, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: add button for own admin module action to admin products grid

Magento: Adding Custom Buttons to Admin Products Grid

Introduction

In Magento, adding buttons to admin grids is a common task when you want to extend core functionalities or implement custom features. While the process seems straightforward, there are often challenges when trying to call admin controller actions from different modules.

This post will guide you through adding a button to the product admin grid and having it call an adminhtml action in your own module. We’ll also discuss why calling frontend actions works differently than adminhtml actions.

Understanding the Issue

Calling Frontend Actions

When adding a button to an admin grid, you can specify the URL using the format <module>/index/action. This syntax will correctly call frontend controller actions from your custom module. For example:

$button = $this->getLayout()->createBlock('Magento\Backend\Block\Widget\Button')
    ->setData([
        'label' => __('My Custom Action'),
        'on_click' => "window.location.href='" . $this->urlBuilder->getUrl('mymodule/index/myaction') . "'",
        'class' => 'action-secondary'
    ]);

Calling Adminhtml Actions - The Challenge

However, when you try to call adminhtml controller actions using the format <module>/adminhtml/action, it doesn’t work as expected. This is because Magento’s routing configuration for frontend and adminhtml namespaces differs.

The default Magento routes use {$namespace}_{$module}_controller_$action for adminhtml controllers, which conflicts with your custom module structure.

The Solution: Overriding Adminhtml Routing

To resolve this issue, you need to override the adminhtml routing configuration for your custom module. Here’s a step-by-step guide:

1. Create a new di.xml file in your custom module

In your module directory (e.g., /app/code/Vendor/MyModule), create a new XML file named di.xml.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Backend\App\Action\Context">
        <arguments>
            <argument name="actionFlag" xsi:type="array">
                <item name="actionsAllowedInAdminhtml" xsi:type="string">MyModule\Adminhtml\Controller\Index\MyAction</item>
            </argument>
        </arguments>
    </type>
</config>

Replace MyModule with your actual module name and MyAction with the specific adminhtml action you want to call.

2. Define the Adminhtml Controller

Create the corresponding adminhtml controller in the required namespace:

namespace Vendor\MyModule\Controller\Adminhtml\Index;

use Magento\Backend\App\Action;
use Magento\Framework\Controller\ResultFactory;

class MyAction extends Action
{
    public function execute()
    {
        // Your custom action logic here

        return $this->resultFactory->create(ResultFactory::TYPE_PAGE);
    }
}

3. Add the Button to the Product Admin Grid

In your module’s layout XML file (e.g., app/code/Vendor/MyModule/view/adminhtml/layout/catalog_product_index.xml), add a new button:

<referenceBlock name="product actions" removed="true">
    <block class="Magento\Backend\Block\Widget\Button"
           name="mycustombutton"
           template="Vendor_MyModule::widget/button.phtml"
           before="-">
        <arguments>
            <argument name="label" xsi:type="string" translate="true">My Custom Action</argument>
            <argument name="on_click" xsi:type="string" translate="false">window.location.href='<?php echo $this->getUrl('adminhtml/mymodule/index/myaction') ?>'; return false;</argument>
            <argument name="class" xsi:type="string">action-secondary</argument>
        </arguments>
    </block>
</referenceBlock>

Replace Vendor_MyModule::widget/button.phtml with the path to your custom button template.

Conclusion

By following these steps, you can successfully add a custom button to the admin products grid and have it call an adminhtml controller action in your own module. This process requires understanding Magento’s routing configuration for both frontend and adminhtml namespaces.

Remember to always test your changes thoroughly before deploying them to production environments. For more advanced customizations and best practices, consider exploring resources like Laravel Company for detailed guides on Magento development.

If you have any further questions or need assistance with specific modules, feel free to ask in the comments below. Happy coding!

Catalog Extensions Themes Admin

Our network