Magento 2 add a url to column under grid in admin panel and add button to admin panel

Firstly let see how to add a url to column in grid. Suppose you want to have an anchor tag on a text in grid then you need to add a column as shown below.

$this->addColumn(
                'view', [
            'header' => __('View'),
            'type' => 'action',
            'getter' => 'getIncrementId',
            'actions' => [
                [
                    'caption' => __('View'),
                    'url' => [
                        'base' => 'invoice/index/viewdetails',
                    ],
                    'field' => 'increment_id'
                ]
            ],
            'filter' => false,
            'sortable' => false,
            'index' => 'increment_id',
            'header_css_class' => 'col-action',
            'column_css_class' => 'col-action'
        ]);

Above we have added a ‘view‘ text which has url ‘invoice/index/viewdetails’ and parameter increment_id associated with it, thus above code will add url which will look like:-
http://XXX/admin/invoice/index/viewdetails/increment_id/000000001/
For url you need to add a controller file and layout to perform your required functionality.

Magento2 add button in admin page
Let’s add send email and print button in our custom module in admin panel, after this we will learn to download pdf and send email on click of these button. So to add a button we need to create a form. For that we will create a form container (Block\Adminhtml\customer\Invoice\View.php), currently this the path to our block file you can modify it as per your need. Below the code that needs to be added:-

namespace W3solver\Invoice\Block\Adminhtml\Customer\Invoice;

use Magento\Customer\Controller\RegistryConstants;

class View extends \Magento\Backend\Block\Widget\Form\Container {

    protected $_coreRegistry = null;

    private $adminHelper;

    private $_invoiceCollectionFactory;
    protected $invoice;
    protected $_backendSession;

    public function __construct(
    \Magento\Backend\Block\Widget\Context $context, \Magento\Backend\Model\Auth\Session $backendSession, \Magento\Framework\Registry $registry, \Magento\Sales\Model\Order\InvoiceFactory $invoiceCollectionFactory, array $data = []
    ) { 
        $this->_coreRegistry = $registry;
        $this->_backendSession = $backendSession;
        $this->_invoiceCollectionFactory = $invoiceCollectionFactory;
        parent::__construct($context, $data);
    }

    protected function _construct() {
        $this->_objectId = 'entity_id';
        $this->_blockGroup = 'W3solver_Invoice';
        $this->_controller = 'adminhtml_customer_invoice';
        $this->_mode = 'view';
        $this->_session = $this->_backendSession;

        parent::_construct();

        $this->buttonList->remove('save');
        $this->buttonList->remove('reset');

        $this->addButton(
                'send_notification', [
            'label' => __('Send Email'),
            'class' => 'send-email',
            'onclick' => 'confirmSetLocation(\'' . __(
                    'Are you sure you want to send a invoice email to customer?'
            ) . '\', \'' . $this->getEmailUrl() . '\')'
                ]
        );

        $this->buttonList->add(
                'print', [
            'label' => __('Print'),
            'class' => 'print',
            'onclick' => 'setLocation(\'' . $this->getPrintUrl() . '\')'
                ]
        );
    }

    public function getEmailUrl() {
        return $this->getUrl('invoice/index/email', ['increment_id' => $this->_request->getParam('increment_id')]);
    }

    public function getPrintUrl() {
        return $this->getUrl('invoice/index/print', ['increment_id' => $this->_request->getParam('increment_id')]);
    }
}


Above you can see that we have added two button in constructor and provided URL to both of them. One more file is needed to be added on following path (Block\Adminhtml\Customer\Invoice\View\Form.php). Below is the code for the same:-

namespace W3solver\Invoice\Block\Adminhtml\Customer\Invoice\View;
class Form extends \Magento\Backend\Block\Widget {
}

Leave a Comment.