In Magento 2 how to show an image in the custom column on the sales order grid. I added a custom dynamic column in sales order grid Magento 2
Vendor\Module\Ui\Component\Listing\DataProvider.php
<?php
namespace Vendor\Module\Ui\Component\Listing;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\Search\ReportingInterface;
use Magento\Framework\Api\Search\SearchCriteria;
use Magento\Framework\Api\Search\SearchCriteriaBuilder;
use Magento\Framework\Api\Search\SearchResultInterface;
use Magento\Framework\App\RequestInterface;
class DataProvider extends \Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider
{
const IMAGE_HEIGHT = '25';
const IMAGE_STYLE = 'display: block;';
public function __construct(
$name,
$primaryFieldName,
$requestFieldName,
ReportingInterface $reporting,
SearchCriteriaBuilder $searchCriteriaBuilder,
RequestInterface $request,
FilterBuilder $filterBuilder,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
array $meta = [],
array $data = []
) {
parent::__construct($name, $primaryFieldName, $requestFieldName, $reporting, $searchCriteriaBuilder, $request, $filterBuilder, $meta, $data);
$this->orderRepository = $orderRepository;
$this->_storeManager = $storeManager;
}
public function getData()
{
$height = self::IMAGE_HEIGHT;
$style = self::IMAGE_STYLE;
$data = parent::getData();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$collectionFlag = $objectManager->create('Vendor\Module\Model\ResourceModel\Orderflag\Collection');
foreach ($data['items'] as &$item) {
$collection = $objectManager->create('Vendor\Module\Model\ResourceModel\Ordercolumn\Collection')
->addFieldToSelect(array('column_id','column_name'));
if($collection->getSize()){
foreach( $collection as $column){
foreach($collectionFlag as $flagValue) {
$srcImage = $this->_storeManager->getStore()
->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) .$flagValue->getData('image_icon');
$image = sprintf("<img src='".$srcImage."' height='".$height."'>");
if($column->getData('column_id') == $flagValue->getData('column_name')):
$item[$column->getData('column_id')] = $image;
//echo $item[$column->getData('column_id')];
else:
$item[$column->getData('column_id')]= 'N/A';
endif;
}
}
}
}
return $data;
}
}
sales_order_grid.xml
<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<dataSource name="sales_order_grid_data_source">
<dataProvider class="Vendor\Module\Ui\Component\Listing\DataProvider" name="sales_order_grid_data_source" />
</dataSource>
<columns name="sales_order_columns" class="Vendor\Module\Ui\Component\Listing\Columns" />
</listing>
Vendor\Module\Ui\Component\Listing\Columns.php
<?php
namespace Vendor\Module\Ui\Component\Listing;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Vendor\Module\Ui\Component\Listing\Column\CustomColumnCreator;
class Columns extends \Magento\Ui\Component\Listing\Columns
{
/**
* @var CustomColumnCreator
*/
private $columnCreator;
public function __construct(
ContextInterface $context,
CustomColumnCreator $columnCreator,
array $components = [],
array $data = []
) {
parent::__construct($context, $components, $data);
$this->columnCreator = $columnCreator;
}
public function prepare()
{
$billingNameColumn = $this->components['billing_name'];
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$collection = $objectManager->create('Vendor\Module\Model\ResourceModel\Ordercolumn\Collection')
->addFieldToSelect(array('column_id','column_name'));
if($collection->getSize()){
foreach ( $collection as $flag){
$column = $this->columnCreator->addColumnFromExistingColumn(
$billingNameColumn,
$flag->getData('column_id'),
$flag->getData('column_name'),
11
);
$this->addComponent($flag->getData('column_id'), $column);
}
}
parent::prepare();
}
}
I added a custom dynamic column in sales order grid Magento 2
Reference: Add custom dynamic column in sales order grid Magento 2