I'm trying to add a column on the product grid with the product frontend url.. I had added the grid by the below codes,
etc/adminhtml/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\Reporting">
<plugin name="products_grid_collection"/>
</type>
<type name="ProductGrid">
<arguments>
<argument name="frontUrlModel" xsi:type="object" shared="false">Magento\Framework\Url</argument>
</arguments>
</type>
etc/adminhtml/module.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="SouqTime_ProductViewToProductGrid" setup_version="1.0.0">
</module>
Ui\Component\Columns\ProductGrid.php
<?php
namespace SouqTime\ProductViewToProductGrid\Ui\Component\Columns;
use \Magento\Framework\View\Element\UiComponent\ContextInterface;
use \Magento\Framework\View\Element\UiComponentFactory;
use \Magento\Ui\Component\Listing\Columns\Column;
class ProductGrid extends Column
{
protected $_storeManager;
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager,
array $components = [], array $data = []
)
{
$this->_storeManager = $storeManager;
parent::__construct($context, $uiComponentFactory, $components, $data);
}
public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
$obManager = \Magento\Framework\App\ObjectManager::getInstance();
$target = $this->_storeManager->getStore()->getBaseUrl();
$stManager = $obManager->get('Magento\Catalog\Model\Product');
$fieldName = $this->getData("name");
foreach ($dataSource["data"]["items"] as $key => $item) {
$product=$stManager->load($item["entity_id"]);
$url_key = $product->getUrlInStore();
$dataSource["data"]["items"][$key][$fieldName] = $url_key;
}
}
return $dataSource;
}
}
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="preview" class="SouqTime\ProductViewToProductGrid\Ui\Component\Columns\ProductGrid">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">View</item>
</item>
</argument>
</column>
</columns>
</listing>
Now,Above code return the first product's url to all products like below
https://i.sstatic.net/y9nkG.jpg
Please Anyone help me to fix this.. Thanks in advance.