1

I have created custom admin grid which contains column name sku.

How to make this sku field value into url.That url go for product edit page.

In layout file

 <column name="product_sku" class="Xxx\Stock\Ui\Component\Listing\Columns\Product">
            <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">Sku</item>
                </item>
            </argument>
        </column>

Product.php

 public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            $fieldName = $this->getData('name');
            foreach ($dataSource['data']['items'] as & $item) {
                if (isset($item[$fieldName])) {
                    $html = "<a href='" . $this->context->getUrl('adminhtml/catalog_product/edit',['id'=>$item[$fieldName]]) . "'>";
                    $html .= __('Product Page');
                    $html .= "</a>";
                    $item[$fieldName] = $html;
                }
            }
        }

        return $dataSource;
    }

Its not working....

1 Answer 1

2

The URL should be specified as 'catalog/product/edit' and not 'adminhtml/catalog_product/edit'

To get product id from product sku, make below changes in class -

Xxx\Stock\Ui\Component\Listing\Columns\Product

public function __construct(\Magento\Catalog\Model\ProductFactory $productFactory, ...)
{
    ...
    $this->productFactory = $productFactory;
    $this->product = $this->productFactory->create();
}
public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            $fieldName = $this->getData('name');
            foreach ($dataSource['data']['items'] as & $item) {
                if (isset($item[$fieldName])) {
                   $productId = $this->product->getIdBySku($item[$fieldName]);
                    $html = "<a href='" . $this->context->getUrl('catalog_product/edit',['id'=>$productId]) . "'>";
                    $html .= __('Product Page');
                    $html .= "</a>";
                    $item[$fieldName] = $html;
                }
            }
        }

        return $dataSource;
    }

Furthermore, you will need to specify that the column is HTML otherwise it will print out the HTML of the link itself instead of creating a link. This is done using the bodyTmpl as in this example

<column name="myColumn">
    <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="sorting" xsi:type="string">asc</item>
            <item name="label" xsi:type="string" translate="true">My Column</item>
        </item>
    </argument>
</column>
7
  • Did my solution work for you?
    – Shireen N
    Mar 20, 2018 at 11:42
  • Yes ...I'm get the link without html tag.But if I'm clicking that link...It not go for desire page.........
    – Mahi M
    Mar 20, 2018 at 11:45
  • so on which page its redirecting you?
    – Shireen N
    Mar 20, 2018 at 11:51
  • The link go for product edit page when i click the 'open link to new tab'..
    – Mahi M
    Mar 20, 2018 at 11:54
  • Debug the code and check the value of the id which you are passing. As seen from your code $item[$fieldName] should return the correct value which you have retrieved for that column field
    – Shireen N
    Mar 20, 2018 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.