0

I added a button in the Admin under Catalog->Category. Now I would like this button to have the URL or click function that redirects to my custom controller in the admin. But I can't seem to generate the proper URL format. I tested the Custom Controller and the endpoints works. I just need to incorporate it with a button. So whenever I click this button it redirects or calls an ajax to this button (but redirecting is ok for now) below is how I created the button

File location: /app/code/Vendor/Module/Block/Adminhtml/Category/Edit/ClearCacheButton.php

<?php
namespace Vendor\Module\Block\Adminhtml\Category\Edit;

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
use Magento\Catalog\Block\Adminhtml\Category\AbstractCategory;

/**
 * Class ClearCacheButton
 */
class ClearCacheButton extends AbstractCategory implements ButtonProviderInterface
{    
    /**
     * Clear Cache button
     *
     * @return array
     */
    public function getButtonData()
    {
        $category = $this->getCategory();
        $categoryId = (int)$category->getId();    
        $url = $this->getUrl('vendormodule/menu/index'); // This does not generate the desired URL for the custom module
        if ($categoryId) {
            return [
                'id' => 'clear_cache',
                'label' => __('Clear Cache Menu'),
                'onclick' => "setLocation(\'" . $url . "\')",          
                'class' => 'save',
                'sort_order' => 10,
                'url' => $url
            ];
        }

        return [];
    }
}       

Also added the ui_component as shown below

File location: /app/code/Vendor/Module/view/adminhtml/ui_component/category_form.xml

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <settings>
        <buttons>
            <button name="clear_cache" class="Vendor\Module\Block\Adminhtml\Category\Edit\ClearCacheButton"/>
        </buttons>
    </settings>
</form>

Here is the routes.xml

File location: /app/code/Vendor/Module/etc/adminhtml/routes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <!--Use router 'admin' for admin route -->
    <router id="admin">
        <!--Define a custom route with id and frontName -->
        <route id="vendormodule" frontName="vendormodule">
            <!--The module which this route match to-->
            <module name="Vendor_Module"/>
        </route>
    </router>
</config>

Note: Custom controller definition is correct as I have tested it and didn't bother to add it here.

  • You don't actually show your controller file in your question. I have a button working from a form to a controller which enacts a redirect, and doesn't require implementing 'ButtonRedirectInterface', but to answer I would need to know where your Controller was in your project path – Scott Anderson Oct 15 at 14:38
0

Try with the following code. Hope this will help you.

public function getButtonData()
{
    $category = $this->getCategory();
    $categoryId = (int)$category->getId();  
    if ($categoryId) {
        return [
            'id' => 'clear_cache',
            'label' => __('Clear Cache Menu'),
            'on_click' => sprintf("location.href = '%s';", $this->getCustomUrl()),       
            'class' => 'save',
            'sort_order' => 10,
            'url' => $url
        ];
    }

    return [];
}



public function getCustomUrl()
{
    return $this->getUrl('frontname/yourControllerName/Action');
}
| improve this answer | |
  • This is almost the same with my approach it's just that you use sprintf instead of setlocation. I was thinking of different approach if possible. – MadzQuestioning Aug 4 '19 at 23:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.