Adding Hyperlinks to Magento2 Product Grid via Custom Extension
As an experienced Magento developer, I often receive requests from clients who want to extend the functionality of their admin panel. One common requirement is to add hyperlinks within the product grid in the backend. In this blog post, we’ll guide you through the process of adding a link column to your custom Magento 2 extension.
The Current Scenario: Plain Text Column
You’ve already created an XML file named product_listing.xml located at app\code\MyCompany\ExampleAdminNewPage\view\adminhtml\ui_component. Your current code looks like this:
<?xml version="1.0"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<columns name="product_columns" class="Magento\Catalog\Ui\Component\Listing\Columns">
<column name="sku">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="add_field" xsi:type="boolean">true</item>
<item name="label" xsi:type="string" translate="true">Custom Field2</item>
<item name="sortOrder" xsi:type="number">75</item>
</item>
</argument>
</column>
</columns>
</listing>
This code defines a new column named “sku” in the product grid, but it currently displays only plain text.
The Goal: Hyperlink Instead of Plain Text
You want to replace this plain text with an actual hyperlink that points to your desired URL. To achieve this, we need to modify the column element and its attributes.
Step 1: Update the Column Element
First, let’s update the existing <column> element in your product_listing.xml file:
<column name="sku">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="add_field" xsi:type="boolean">true</item>
<item name="label" xsi:type="string" translate="true">Custom Field2</item>
<item name="sortOrder" xsi:type="number">75</item>
<!-- New attributes added here -->
<item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item>
<item name="dataType" xsi:type="string">text</item>
<item name="renderer" xsi:type="array">
<item name="module" xsi:type="string">MyCompany_ExampleAdminNewPage</item>
<item name="component" xsi:type="string">Magento_Ui/js/grid/columns/columns/column/renderer/default</item>
</item>
</item>
</argument>
</column>
Step 2: Create the Renderer Component
Next, we need to create a renderer component that will handle the hyperlink formatting. In your extension’s directory (i.e., app\code\MyCompany\ExampleAdminNewPage), navigate into the view\adminhtml\ui_component folder and create a new file named skulink.html.
Insert the following code into this new HTML file:
<column class="data-grid-cell-renderer-cell">
<input name="${ $.$name }" type="text" data-bind="text: ${ $ }"/>
</column>
This file defines the renderer component that will be used to format the cell content.
Step 3: Update Your Extension’s Configuration
Now, let’s modify your extension’s configuration file (app\code\MyCompany\ExampleAdminNewPage\etc\module.xml) to include the new renderer component:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="MyCompany_ExampleAdminNewPage" setup_version="1.0.0">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
</module>
<!-- New configuration added here -->
<adminhtml>
<layout>
<updates>
<update handle="catalog_product_index_manage">
<referenceBlock name="product_grid" component="MyCompany_ExampleAdminNewPage/js/grid/columns/column/renderer/default"/>
</update>
</updates>
</layout>
</adminhtml>
</config>
Step 4: Update the Column URL
To include the actual hyperlink, you need to update your extension’s PHP code. Open your app\code\MyCompany\ExampleAdminNewPage\Controller\Adminhtml\Index\Index.php file and modify the execute() method:
public function execute()
{
$this->_view->loadLayout();
$this->_addContent(
$this->_view->getLayout()->createBlock('Magento\Catalog\Block\Adminhtml\Product')
->setData('area', 'adminhtml')
);
$this->_initAction();
$this->_renderLayout();
}
Add the following PHP code within the execute() method to update the column URL:
$productGrid = $this->getLayout()->getBlock('catalog.product.grid')->getCollection();
foreach ($productGrid as $item) {
$item->setData('sku_url', 'http://your-desired-url.com/' . $item->getSku());
}
Step 5: Test Your Extension
After making these changes, clear the Magento cache and refresh your admin panel. You should now see a new column named “Custom Field2” in the product grid with hyperlinks instead of plain text.
Remember to replace 'http://your-desired-url.com/' with the actual URL you want to link each SKU to.
Conclusion
In this blog post, we’ve demonstrated how to add a hyperlink column to your custom Magento 2 extension’s product grid. By following these steps and updating your XML files and PHP code accordingly, you can seamlessly integrate this new functionality into your admin panel.
At Laravel Company (https://laravelcompany.com), we specialize in providing high-quality Magento development services. Our team of experienced developers is well-versed in extending Magento’s core features to meet our clients’ unique business requirements. Contact us today to learn more about how we can help you customize your e-commerce platform.
Happy coding!