Rendering HTML tags in Magento’s admin product grid is a common requirement for customizing the appearance of your catalog management. In this post, we’ll guide you through the process of creating an anchor link in the product grid view.
Step 1: Define the Column in layout.xml
In your module’s view/adminhtml/layout folder, create or update your layout.xml file to define a new column for your grid:
<column name="column_name" class="Vendor\Module\Ui\Component\Listing\Column\link">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>
<item name="filter" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Label</item>
<item name="sortOrder" xsi:type="number">50</item>
</item>
</argument>
</column>
Replace Vendor\Module with your actual module’s vendor and namespace. The bodyTmpl attribute specifies the template for rendering the cell content.
Step 2: Create the Column Class
In your module, create a new file under Ui/Component/Listing/Column named link.php. This class is responsible for preparing the data for the grid cell:
namespace Vendor\Module\Ui\Component\Listing\Column;
class Link implements \Magento\Framework\View\Element\UiComponentInterface
{
public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
foreach ($dataSource['data']['items'] as &$item) {
$html = '<a href="my_link.html" target="_blank">' . __('Label') . '</a>';
$item[$this->getData('name')] = $html;
}
}
return $dataSource;
}
}
The prepareDataSource method is used to modify the data before it’s sent to the grid template. In this case, we’re creating an anchor link for each item in the grid.
Step 3: Understand the Current Issue
Your current code is almost correct, but there are a few issues:
- The
$htmlvariable you’re using doesn’t contain any actual HTML content. - The
item[$this->getData('name')]line is overwriting the entire item object with the HTML string.
Let’s fix these issues in the next step.
Step 4: Correct the DataSource Code
To correctly render the anchor link, update your datasource code as follows:
$html = '<a href="' . $item->getData('link') . '" target="_blank">' . __('Label') . '</a>';
$item->setData($this->getData('name'), $html);
Here’s what we’ve done:
- We’re assuming you have a
linkattribute in your product that contains the URL for the anchor. - The
__()function is used to translate the label text if needed. - Instead of overwriting the entire item object, we use the
setDatamethod to add the HTML content to the specified column name.
Conclusion
In this post, we’ve demonstrated how to render an anchor link in Magento’s admin product grid. The key steps are:
- Define a new column in your layout.xml file.
- Create a custom column class for preparing the data.
- Modify the datasource code to include the HTML content.
Remember that this is a basic example; you may need to adjust it according to your specific requirements and Magento version.
If you’re looking to learn more about customizing Magento’s admin interface, check out our guide on How to Create Custom Admin Grids in Magento.