Magento – Magento 2 add new Button in admin Controller

adminhtmlcontrollerscustom-buttongridmagento2

I am working on module which will work for sync product from Third party Api.I have already created admin controller page but don't have Idea how to add button Sync Products whose action will call a controller file .
Have anyone idea how to add new custom button with action controller?

I have added menu in admin section with Parent Catalog->

Admin menu is

app/code/Vendorname/Syncfromshopify/etc/adminhtml/menu.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
    <menu>
        <add id="Vendorname_Syncfromshopify::sync_product" title="Sync From Shopiofy" module="Vendorname_Syncfromshopify" parent="Magento_Catalog::catalog" sortOrder="30"  resource="Vendorname_Syncfromshopify::sync_product" action="syncproductshopify/syncproduct/" />
    </menu>
</config>

My controller is

app/code/Vendorname/Syncfromshopify/Controller/Adminhtml/Syncproduct/Index.php

namespace Vendorname\Syncfromshopify\Controller\Adminhtml\Syncproduct;

use Magento\Backend\App\Action\Context;
use Magento\Framework\View\ResultPageFactory;


class Index extends \Magento\Backend\App\Action

{
    /**
    * @var PageFactory
    */
    public function execute()
    {
        $this->_view->loadLayout();
        $this->_view->renderLayout();
    }

}

My above controller is working fine and redirecting to admin page.

Now I want to add a button (ABC name) and in action of another controller which will responsible for sync data from third party and Insert in Magento.

So How we will create new button in admin controller and give action?

Best Answer

Create a file in Vendorname\Module\Block\Adminhtml\CustomButtom.php

<?php
namespace Vendorname\Module\Block\Adminhtml\Buttom;

use \Magento\Backend\Block\Widget\Context;
use \Magento\Backend\Model\UrlInterface;

class CustomButtom extends \Magento\Backend\Block\Widget\Container {
    protected $_backendUrl;

    /**
     * @param \Magento\Backend\Block\Widget\Context $context
     * @param array $data
     */
    public function __construct(
        Context $context,
        UrlInterface $backendUrl,
        array $data = []
    ) {
        $this->_backendUrl = $backendUrl;
        parent::__construct($context, $data);
    }

    /**
     * Block constructor adds buttons
     *
     */
    protected function _construct() {
        $this->addButton(
            'import_ref',
            $this->getButtonData()
        );
        parent::_construct();
    }

    /**
     * Return button attributes array
     */
    public function getButtonData() {
        $url = $this->_backendUrl->getUrl("module/page/index");
        return [
            'label' => __('Custom buttom label'),
            'on_click' => 'setLocation("'.$url.'")',
            'class' => 'primary'
        ];
    }
}

In your layout just need to add the buttom

Vendorname\Module\view\adminhtml\layout\name-layout.xml

<block class="Vendorname\Module\Block\Adminhtml\Status\CustomButtom" name="CustomButtom"/>
Related Topic