0

in need to add state columns and get state name from region id so i used object manager for it but that not right way i convert that in constructor method the data of state comes but the column of grid was hide. enter image description here

how can i add constructor method in this ??

<?php

namespace vendor\mpdule\Ui\Component\Listing\Column;

use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Ui\Component\Listing\Columns\Column;


class State extends Column
{
    public function prepareDataSource(array $dataSource)
    {   

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $countryFactory = $objectManager->get('Magento\Directory\Model\CountryFactory')->create();      
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        if(isset($dataSource['data']['items'])) {
            foreach($dataSource['data']['items'] as &$items) {

                $regionId = $items['state'];
                $region = $objectManager->create('Magento\Directory\Model\Region')
                        ->load($regionId);


                
                $items['state'] = $region['name'];           
            }
        }
        return $dataSource;
    }
}

1 Answer 1

2

Please update you code with below code.

<?php

namespace vendor\mpdule\Ui\Component\Listing\Column;

use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Directory\Model\Region;


class State extends Column
{
    /**
     * @var Region
     */
    protected $region;

    /**
     * @param ContextInterface $context
     * @param UiComponentFactory $uiComponentFactory
     * @param Region $region
     * @param array $components
     * @param array $data
     */
    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        Region $region,
        array $components = [],
        array $data = [],
    ) {
        $this->region = $region;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

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

                $regionId = $items['state'];
                $region = $this->region->load($regionId);
                
                $items['state'] = $region['name'];           
            }
        }
        return $dataSource;
    }
}

Note: I removed "$countryFactory" because it don't used anywhere in your code. if you need to use it please define same as like "Region".

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.