Adding Custom Links in Magento2 Collection Grid
In Magento2, you can easily add custom links to your collection grid by extending the existing grid block and adding new columns with custom link functionality.
Step-by-Step Guide
1. Prepare Your Module Structure
Ensure that you have set up your module in the correct structure:
App/Code/Inchoo/Helloworld/etc/module.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Inchoo_Helloworld" setup_version="1.0.0">
<sequence>
<module name="Magento_Backend"/>
</sequence>
</module>
</config>
2. Create Your Model and Resource Model
Create the necessary model and resource model for your collection:
App/Code/Inchoo/Helloworld/Model/Orderprocessing.php
namespace Inchoo\Helloworld\Model;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\DataObject\IdentityInterface;
class Orderprocessing extends AbstractModel implements IdentityInterface
{
const CACHE_TAG = 'inchoo_orderprocessing';
public function _construct()
{
$this->_init('Inchoo\Helloworld\Model\ResourceModel\Orderprocessing');
}
...
}
App/Code/Inchoo/Helloworld/Model/ResourceModel/Orderprocessing.php
namespace Inchoo\Helloworld\Model\ResourceModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
use Magento\Framework\DataObject\IdentityInterface;
class Orderprocessing extends AbstractDb implements IdentityInterface
{
const CACHE_TAG = 'inchoo_orderprocessing';
protected function _construct()
{
$this->_init('inchoo_orderprocessing', 'entity_id');
}
...
}
3. Create Your Collection
App/Code/Inchoo/Helloworld/Model/ResourceModel/Orderprocessing/Collection.php
namespace Inchoo\Helloworld\Model\ResourceModel\Orderprocessing;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Magento\Framework\DataObject\IdentityInterface;
class Collection extends AbstractCollection implements IdentityInterface
{
const CACHE_TAG = 'inchoo_orderprocessing';
protected $_eventPrefix = 'inchoo_orderprocessing_collection';
protected $_eventObject = 'orderprocessing_collection';
public function _construct()
{
$this->_map['fields']['entity_id'] = 'main_table.entity_id';
$this->_init('Inchoo\Helloworld\Model\Orderprocessing', 'Inchoo\Helloworld\Model\ResourceModel\Orderprocessing');
$this->_renderFilters();
}
...
}
4. Create Your Layout XML File
App/Code/Inchoo/Helloworld/view/adminhtml/layout/helloworld_orderprocessing_index.xml
This is where you define your grid structure and add the custom links:
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="styles"/>
<body>
<referenceContainer name="content">
<!-- Your grid definition goes here -->
</referenceContainer>
</body>
</page>
5. Add Custom Links in the Grid
To add a custom link, you need to extend the existing grid block and add a new column with the desired functionality:
App/Code/Inchoo/Helloworld/Block/Adminhtml/Orderprocessing.php
namespace Inchoo\Helloworld\Block\Adminhtml;
use Magento\Backend\Block\Widget\Grid\Container;
use Magento\Framework\App\RequestInterface;
class Orderprocessing extends Container
{
public function __construct(
RequestInterface $request,
\Magento\Framework\View\Element\Template\Context $context,
\Inchoo\Helloworld\Model\ResourceModel\Orderprocessing $resource = null,
\Magento\Framework\Registry $registry = null,
array $data = []
) {
parent::__construct($context, $request, $resource, $registry, $data);
$this->_blockGroup = 'Inchoo_Helloworld';
$this->_controller = 'adminhtml_orderprocessing';
}
protected function _toHtml()
{
// Add your custom link functionality here
return parent::_toHtml();
}
}
Conclusion
Adding custom links to a Magento2 collection grid allows you to enhance the user interface and provide more functionality directly from the grid view. By following this guide, you can easily extend an existing grid block and add new columns with custom link functionality.
This process requires knowledge of Magento’s layout XML files, model and resource model creation, and extending existing blocks. It is a common task for Magento developers and forms part of the core framework capabilities.
Check out more Magento2 development tips on our blog at https://laravelcompany.com/blog/magento2-development-tips to learn more about advanced Magento techniques.