1

hello everyone I want to add product edit link on product name which is showing in admin grid below code is for the display product name index.xml

<column name="entity_id" class="Magneto\SampleRequest\Ui\Component\Listing\Column\Products"> 
        <argument name="data" xsi:type="array"> 
        <item name="config" xsi:type="array"> 
        <item name="visible" xsi:type="boolean">true</item> 
        <item name="label" xsi:type="string" translate="true">Products Name</item> 
        </item> 
        </argument> 
        </column>

products.php

<?php
      namespace Magneto\SampleRequest\Ui\Component\Listing\Column;
    use \Magento\Catalog\Api\ProductRepositoryInterface
  use\Magento\Framework\View\Element\UiComponent\ContextInterface;
  use \Magento\Framework\View\Element\UiComponentFactory;
  use \Magento\Ui\Component\Listing\Columns\Column;
   class Products extends Column
       {
      protected $_ProductRepository;
  public function __construct(
     ContextInterface $context,
    UiComponentFactory $uiComponentFactory,
   ProductRepositoryInterface $ProductRepository,
    array $components = [],
     array $data = [])
       {
      $this->_ProductRepository = $ProductRepository;
    parent::__construct($context, $uiComponentFactory, $components, 
   $data);
   }
       public function prepareDataSource(array $dataSource)
       {
if (isset($dataSource['data']['items'])) {
    foreach ($dataSource['data']['items'] as $key => $items) {
        $product = $this->_ProductRepository->getById($items["entity_id"]);
        $dataSource['data']['items'][$key]['entity_id'] = $product->getName(); //to get product name
    }
}
return $dataSource;
    }
     }

now i want to display product edit link on product name click as you can product name is displaying

1 Answer 1

1

You need to update both files

index.xml with below code

<column name="entity_id" class="Magneto\SampleRequest\Ui\Component\Listing\Column\Products"> 
    <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="visible" xsi:type="boolean">true</item> 
            <item name="label" xsi:type="string" translate="true">Products Name</item>
        </item> 
    </argument> 
</column>

and products.php with below code

    <?php
namespace Magneto\SampleRequest\Ui\Component\Listing\Column;
    use \Magento\Catalog\Api\ProductRepositoryInterface;
    use\Magento\Framework\View\Element\UiComponent\ContextInterface;
    use \Magento\Framework\View\Element\UiComponentFactory;
    use \Magento\Ui\Component\Listing\Columns\Column;
    use \Magento\Framework\UrlInterface;
    class Products extends Column
   {
        private $urlBuilder;
        const PRODUCT_URL_PATH_EDIT = 'catalog/product/edit';
        protected $_ProductRepository;
        public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        ProductRepositoryInterface $ProductRepository,
        UrlInterface $urlBuilder,
        array $components = [],
        array $data = [])
        {
            $this->_ProductRepository = $ProductRepository;
            $this->urlBuilder = $urlBuilder;
            parent::__construct($context, $uiComponentFactory, $components, 
            $data);
        }
        public function prepareDataSource(array $dataSource)
        {
            if (isset($dataSource['data']['items'])) {
                foreach ($dataSource['data']['items'] as $key => $items) {
                    $product = $this->_ProductRepository->getById($items["entity_id"]);
                    $dataSource['data']['items'][$key]['entity_id'] = html_entity_decode('<a href="'.$this->urlBuilder->getUrl(self::PRODUCT_URL_PATH_EDIT, ['id' => $product->getId()]).'">'.$product->getName().'</a>');
                }
            }
            return $dataSource;
        }
    }

Hope this will help you!

7
  • not working in my module Commented Mar 1, 2019 at 11:32
  • you getting any error after updating above code ?
    – VDC
    Commented Mar 1, 2019 at 11:37
  • no page not found is coming Commented Mar 1, 2019 at 11:44
  • try to check with executing compile and cache flush command
    – VDC
    Commented Mar 1, 2019 at 12:04
  • Undefined variable: item in /var/www/html/magento230/app/code/Magneto/SampleRequest/Ui/Component/Listing/Column/Products.php on line 31 this error is coming Commented Mar 1, 2019 at 12:20

Your Answer

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

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