Question

I am looking for a way to unserialize data (coming from db table) and display it in admin grid (column in ui component)

I have custom module in admin which fetch record from table and one of the column have serialized data

a:2:{s:12:"increment_id";s:5:"60231";s:4:"rate";a:4:{s:6:"points";s:1:"1";s:15:"currency_amount";s:6:"1.0000";s:9:"direction";s:1:"1";s:13:"currency_code";s:3:"NZD";}}

I want to display increment_id from this field in my view/adminhtml/ui_component/modulename_model_listing.xml

Any help please.

Was it helpful?

Solution

In grid xml file add code like

<column name="increment_id" class="Namespace\Module\Ui\Component\Listing\Column\Custom">
      <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="sortable" xsi:type="boolean">false</item>
                <item name="label" xsi:type="string" translate="true">Increment Id</item>
                <item name="sortOrder" xsi:type="number">60</item>
            </item>
        </argument>
</column>

create new file Namespace/Module/Ui/Component/Listing/Column/Custom.php

<?php

namespace Namespace\Module\Ui\Component\Listing\Column;

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

use Magento\Ui\Component\Listing\Columns\Column

/**
 * Class Custom.
 */
class Custom extends Column {

    /**
     * Escaper.
     *
     * @var \Magento\Framework\Escaper
     */
    protected $escaper;
    protected $helper;

    /**
     * Constructor.
     *
     * @param ContextInterface   $context
     * @param UiComponentFactory $uiComponentFactory
     * @param Escaper            $escaper
     * @param array              $components
     * @param array              $data
     */
    public function __construct(
    ContextInterface $context, UiComponentFactory $uiComponentFactory, Escaper $escaper, array $components = [], \Namespace\Module\Helper\Data $helper, array $data = []
    ) {
        $this->escaper = $escaper;
        $this->helper = $helper;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    /**
     * Prepare Data Source.
     *
     * @param array $dataSource
     *
     * @return array
     */
    public function prepareDataSource(array $dataSource) {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as &$item) {
                $item[$this->getData('name')] = $this->prepareItem($item);
            }
        }

        return $dataSource;
    }

    /**
     * Get data.
     *
     * @param array $item
     *
     * @return string
     */
    protected function prepareItem(array $item) {
        $data = unserialize($item['database_field']);

        $id = $data['increment_id'];
        return $id;
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top