Magento 2 adding a custom link in a new column to product_listing.xml
-
10-03-2021 - |
Question
Trying to add a new column with a link into magento 2 product_listing.xml that passes entity id to a custom module.
I have created the Ui Component [company]/[module]/Ui/Component/Listing/Column/EditActions.php
(see below), however I do not know how to add it to the layout. [company]/[module]/view/adminhtml/ui_component/product_listing.xml
See screenshot for an rough mock up https://prnt.sc/os44w1
<?php
namespace [company]\[module]\Ui\Component\Listing\Column;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Cms\Block\Adminhtml\Page\Grid\Renderer\Action\UrlBuilder;
use Magento\Framework\UrlInterface;
class EditActions extends Column {
/** @var UrlBuilder */
protected $actionUrlBuilder;
/** @var UrlInterface */
protected $urlBuilder;
/**
* @var string
*/
private $_viewUrl = '[router]/content/view';
/**
* @param ContextInterface $context
* @param UiComponentFactory $uiComponentFactory
* @param UrlBuilder $actionUrlBuilder
* @param UrlInterface $urlBuilder
* @param array $components
* @param array $data
* @param string $editUrl
*/
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
UrlBuilder $actionUrlBuilder,
UrlInterface $urlBuilder,
array $components = [],
array $data = []
)
{
$this->urlBuilder = $urlBuilder;
$this->actionUrlBuilder = $actionUrlBuilder;
parent::__construct($context, $uiComponentFactory, $components, $data);
}
/**
* Prepare Data Source
*
* @param array $dataSource
* @return array
*/
public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
foreach ($dataSource['data']['items'] as & $item) {
$name = $this->getData('name');
if (isset($item['entity_id'])) {
$item[$name]['contentassist'] = [
'href' => $this->urlBuilder->getUrl($this->_viewUrl, ['id' => $item['entity_id'], 'store_id' => $item['entity_id']]),
'label' => __('link')
];
}
}
}
return $dataSource;
}
}
Solution
Here is an example, module name here is Rendyep_CustomModule
, change it accordingly.
Create app/code/Rendyep/CustomModule/view/adminhtml/ui_component/product_listing.xml
<?xml version="1.0" encoding="utf-8"?>
<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">
<column name="custom_link" class="Rendyep\CustomModule\Ui\Component\Listing\Column\CustomLink" sortOrder="130">
<settings>
<dataType>text</dataType>
<filter>text</filter>
<bodyTmpl>ui/grid/cells/html</bodyTmpl>
<label translate="true">Custom Link</label>
</settings>
</column>
</columns>
</listing>
Create app/code/Rendyep/CustomModule/Ui/Component/Listing/Column/CustomLink.php
<?php
namespace Rendyep\CustomModule\Ui\Component\Listing\Column;
use Magento\Ui\Component\Listing\Columns\Column;
class CustomLink extends Column
{
public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
foreach ($dataSource['data']['items'] as &$item) {
$item['custom_link'] = '<a href="https://google.com/search?q=' . $item['entity_id'] . '">Custom Link</a>';
}
}
return $dataSource;
}
}
Flush cache by executing bin/magento cache:flush
Result:
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange