2

hello everyone I want to display the product name in custom admin grid code is below info.php

$fieldset->addField(
        'entity_id',
        'text',
        [
            'name'        => 'product name',
            'label'    => __('product name'),
            'renderer'  => 'Magneto\BookFreeLayout\Block\Adminhtml\Extension\Grid\Renderer\Productname',
            'required'     => true,

        ]
    ); 

productname.php

<?php
namespace 
Magneto\BookFreeLayout\Block\Adminhtml\Extension\Grid\Renderer;
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 Productname 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;
  }
  }

I have written this code but entity id is displaying entity id is the foreign key

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.