2

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?

11
  • What is your Magento version? Dec 18, 2017 at 11:32
  • Magento version 2.2.1
    – akgola
    Dec 18, 2017 at 11:33
  • But, you want to add the button on product listing page or edit form? Dec 18, 2017 at 12:06
  • I want to add button in custom module,just in an controller.
    – akgola
    Dec 18, 2017 at 12:10
  • Please guide me how to add button in admin controller file and what we will require for it.
    – akgola
    Dec 18, 2017 at 12:11

1 Answer 1

1

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"/>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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