0

I need to override module-catalog\view\adminhtml\ui_component\product_listing.xml file in my custom module. I have copy the core magento file in my module at view\adminhtml\ui_component but its not working. is there anything else i have to do.

0
<?xml version="1.0"?>
<!--
/**
* @copyright Copyright (c) 2016 www.magebuzz.com
*/
-->
<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" class="Magento\Catalog\Ui\Component\Listing\Columns">
       <column name="custom_field">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="add_field" xsi:type="boolean">true</item>
                    <item name="label" xsi:type="string" translate="true">Custom Field</item>
                    <item name="sortOrder" xsi:type="number">75</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

Then created the UI class under [COMPANY]/[MODULE]/Ui/Component/Listing/Column/Status.php

<?php

namespace RLTS\Helloworld\Ui\Component\Listing\Column;

use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;

class Category extends \Magento\Ui\Component\Listing\Columns\Column
{

   public function prepareDataSource(array $dataSource)
   {
      $fieldName = $this->getData('name');
      if (isset($dataSource['data']['items'])) {
         foreach ($dataSource['data']['items'] as & $item) {
         //print_r($item);die;
         $productId=$item['entity_id'];
         //$product=$this->_productloader->create()->load($p_id);
         //$product = $objectManager->get('Magento\Catalog\Model\Product')->loa‌​d($p_id);
         //$productId = $p_id;
         $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
         $product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
         $cats = $product->getCategoryIds();
         //$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
         $categories=array();
         if(count($cats) ){
            foreach($cats as $cat){
               $category = $objectManager->create('Magento\Catalog\Model\Category')->load($cat);
                $categories[]=$category->getName();
            }
         }
         $item[$fieldName]=implode(',',$categories);
         }
      }
   return $dataSource;
   }
}

This is for just DEMO

<column name="amount" class="M [COMPANY]\[MODULE]\Ui\Component\Listing\Column\Status">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="filter" xsi:type="string">text</item>
                <item name="sorting" xsi:type="string">asc</item>
                <item name="sortOrder" xsi:type="number">3</item>
                <item name="label" translate="true" xsi:type="string">Amount</item>
            </item>
        </argument>
    </column>      

namespace [COMPANY]\[MODULE]\Ui\Component\Listing\Column;

use \Magento\Sales\Api\OrderRepositoryInterface;
use \Magento\Framework\View\Element\UiComponent\ContextInterface;
use \Magento\Framework\View\Element\UiComponentFactory;
use \Magento\Ui\Component\Listing\Columns\Column;
use \Magento\Framework\Api\SearchCriteriaBuilder;

class Status extends Column
{
    protected $_orderRepository;
    protected $_searchCriteria;

    public function __construct(ContextInterface $context, UiComponentFactory $uiComponentFactory, OrderRepositoryInterface $orderRepository, SearchCriteriaBuilder $criteria, array $components = [], array $data = [])
    {
        $this->_orderRepository = $orderRepository;
        $this->_searchCriteria  = $criteria;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {

                $order  = $this->_orderRepository->get($item["entity_id"]);
                $status = $order->getData("export_status");

                switch ($status) {
                    case "0":
                        $export_status = "No";
                        break;
                    case "1";
                        $export_status = "Yes";
                        break;
                    default:
                        $export_status = "Failed";
                        break;

                }

                // $this->getData('name') returns the name of the column so in this case it would return export_status
                $item[$this->getData('name')] = $export_status;
            }
        }

        return $dataSource;
    }
}
  • This code will add new column in the grid but what i am trying to do is to add inline Edit in product list grid. I have worked on core file but now i want to use that core file in my module. – Pramod Kumar Sharma Jan 18 '18 at 7:26
  • <column name="amount" class="[COMPANY][MODULE]\Ui\Component\Listing\Columnt"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="filter" xsi:type="string">text</item> <item name="sorting" xsi:type="string">asc</item> <item name="sortOrder" xsi:type="number">3</item> <item name="label" translate="true" xsi:type="string">Amount</item> </item> </argument> </column> – Birjitsinh Zala Jan 18 '18 at 7:28
  • check my code after "This is for just DEMO" line – Birjitsinh Zala Jan 18 '18 at 7:32
  • Thanks for your answer but i don't this kind of functionality. I simply want to override product_listing.xml – Pramod Kumar Sharma Jan 18 '18 at 9:15
  • <column name="custom_field"> this for culumns name available in product table then class="M [COMPANY][MODULE]\Ui\Component\Listing\Column\Status"> not require and also not require below php code – Birjitsinh Zala Jan 18 '18 at 9:18

Your Answer

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

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