0

I created a field in my order grid table, I have an observer set up that will fill the field with a JSON string of the ordered items. I want to have this field rendered to HTML so the ordered items will show on order grid. However, I can not find any information on how to properly render a JSON in UI grid system of Magento 2. I am planning to share it in a public repository so I would like to have the code as clean as possible. I have the following code right now:

namespace Qxs\OrderLinesGrid\Ui\Component;

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

  /**
  * Logger Interface
  *
  * @var \Psr\Log\LoggerInterface
  */
  protected $logger;

  public function __construct(
      \Magento\Framework\View\Element\UiComponent\ContextInterface $context,
      \Magento\Framework\View\Element\UiComponentFactory $uiComponentFactory,
      \Psr\Log\LoggerInterface $logger,
      array $components = [],
      array $data = []
  ){
      $this->logger = $logger;
      parent::__construct($context, $uiComponentFactory, $components, $data);
  }

  public function prepareDataSource(array $dataSource) {
    if (isset($dataSource['data']['items'])) {
      foreach ($dataSource['data']['items'] as & $item) {
        if (!empty($item['order_lines'])) {
          $json = $item['order_lines'];
          $this->logger->debug("JSON Data".$json);
        }
      }
    }
    return $dataSource;
  }
}

However I cannot edit the JSON it returns an empty string, the JSON is showed in the grid but not on var_dump($json). Can anybody guide me in the right direction?

  • Do not add logger in foreach loop. Please add it outside. – Abhishek Panchal Jul 31 '18 at 19:15
1

Try below code. You have to decode your json data to see it. Check your log file. Please make sure that you are getting correct data in function. Please check $dataSource array.

<?php namespace Qxs\OrderLinesGrid\Ui\Component;

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

    /**
    * Logger Interface
    *
    * @var \Psr\Log\LoggerInterface
    */
    protected $logger;

    public function __construct(
        \Magento\Framework\View\Element\UiComponent\ContextInterface $context,
        \Magento\Framework\View\Element\UiComponentFactory $uiComponentFactory,
        \Psr\Log\LoggerInterface $logger,
        array $components = [],
        array $data = []
    ){
        $this->logger = $logger;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    public function prepareDataSource(array $dataSource) {
        if (isset($dataSource['data']['items'])) {
            $this->logger->debug("JSON Data".json_decode($dataSource['data']['items']));
            foreach ($dataSource['data']['items'] as & $item) {
                $json = $item['order_lines'];
                //var_dump($json);
            }
        }
    return $dataSource;
    }
}
  • Many thanks! I have updated my code but log stays empty, what file should it be? – Coen Ponsen Jul 31 '18 at 19:12
  • you should check var/log/debug.log – Abhishek Panchal Jul 31 '18 at 19:12
  • paste your json result in here so that you can see it in proper format. jsonformatter.curiousconcept.com – Abhishek Panchal Jul 31 '18 at 19:13
  • json is validated by the tool, the only entry i have in debug is [2018-07-31 19:14:02] main.DEBUG: cache_invalidate: {"method":"GET","url":"xxx/admin_77z4au/mui/index/render/…":{"tags":["interception","CONFIG"],"mode":"matchingTag"}} [] – Coen Ponsen Jul 31 '18 at 19:15
  • You should add my code.. The code you updated in your question is wrong. Becuase its in foreach loop and you didnt use json_decode – Abhishek Panchal Jul 31 '18 at 19:17

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.