Question

I want to show store view in my custom module grid and it is showing but there is an issue which I want to remove ie. Multiple store view is not showing in grid. Reference image is given below :

enter image description here

Single store is easily showing on grid.

Here is the grid file code

<?php
namespace Custom\Slider\Block\Adminhtml\Slide;

class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
{
    /**
     * @var \Magento\Framework\Module\Manager
     */
    protected $moduleManager;

    /**
     * @var \Custom\Slider\Model\slideFactory
     */
    protected $_slideFactory;

    /**
     * @var \Custom\Slider\Model\Status
     */
    protected $_status;

    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Backend\Helper\Data $backendHelper
     * @param \Custom\Slider\Model\slideFactory $slideFactory
     * @param \Custom\Slider\Model\Status $status
     * @param \Magento\Framework\Module\Manager $moduleManager
     * @param array $data
     *
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Backend\Helper\Data $backendHelper,
        \Custom\Slider\Model\SlideFactory $SlideFactory,
        \Custom\Slider\Model\Status $status,
        \Magento\Store\Model\System\Store $systemStore,
        \Magento\Framework\Module\Manager $moduleManager,
        array $data = []
    ) {
        $this->_slideFactory = $SlideFactory;
        $this->_status = $status;
        $this->_systemStore = $systemStore;
        $this->moduleManager = $moduleManager;
        parent::__construct($context, $backendHelper, $data);
    }

    /**
     * @return void
     */
    protected function _construct()
    {
        parent::_construct();
        $this->setId('postGrid');
        $this->setDefaultSort('slide_id');
        $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);
        $this->setUseAjax(false);
        $this->setVarNameFilter('post_filter');
    }

    /**
     * @return $this
     */
    protected function _prepareCollection()
    {
        $collection = $this->_slideFactory->create()->getCollection();
        $this->setCollection($collection);

        parent::_prepareCollection();

        return $this;
    }

    /**
     * @return $this
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    protected function _prepareColumns()
    {
        $this->addColumn(
            'slide_id',
            [
                'header' => __('ID'),
                'type' => 'number',
                'index' => 'slide_id',
                'header_css_class' => 'col-id',
                'column_css_class' => 'col-id'
            ]
        );



        $this->addColumn(
            'slide_name',
            [
                'header' => __('Name'),
                'index' => 'slide_name',
            ]
        );


        $this->addColumn(
            'slide_status',
            [
                'header' => __('Status'),
                'index' => 'slide_status',
                'type' => 'options',
                'options' => \Custom\Slider\Block\Adminhtml\Slide\Grid::getOptionArray2()
            ]
        );


        $this->addColumn(
            'slide_url',
            [
                'header' => __('Url'),
                'index' => 'slide_url',
            ]
        );

        $this->addColumn(
            'slide_store_view',
            [
                'header' => __('Store View'),
                'index' => 'slide_store_view',
                'type' => 'store',
                'store_all' => true,
                'store_view' => true,
                'sortable' => true,
                //'filter_condition_callback' => [$this, '_filterStoreCondition']
            ]
        );


        $this->addColumn(
            'slide_order',
            [
                'header' => __('Slide Short Order'),
                'index' => 'slide_order'
            ]
        );



        $this->addColumn(
            'slide_use_type',
            [
                'header' => __('Slide Type'),
                'index' => 'slide_use_type',
                'type' => 'options',
                'options' => \Custom\Slider\Block\Adminhtml\Slide\Grid::getOptionArray8()
            ]
        );



        $this->addColumn(
            'edit',
            [
                'header' => __('Edit'),
                'type' => 'action',
                'getter' => 'getId',
                'actions' => [
                    [
                        'caption' => __('Edit'),
                        'url' => [
                            'base' => '*/*/edit'
                        ],
                        'field' => 'slide_id'
                    ]
                ],
                'filter' => false,
                'sortable' => false,
                'index' => 'stores',
                'header_css_class' => 'col-action',
                'column_css_class' => 'col-action'
            ]
        );



           $this->addExportType($this->getUrl('slider/*/exportCsv', ['_current' => true]),__('CSV'));
           $this->addExportType($this->getUrl('slider/*/exportExcel', ['_current' => true]),__('Excel XML'));

        $block = $this->getLayout()->getBlock('grid.bottom.links');
        if ($block) {
            $this->setChild('grid.bottom.links', $block);
        }

        return parent::_prepareColumns();
    }


    /**
     * @return $this
     */
    protected function _prepareMassaction()
    {

        $this->setMassactionIdField('slide_id');
        //$this->getMassactionBlock()->setTemplate('Custom_Slider::slide/grid/massaction_extended.phtml');
        $this->getMassactionBlock()->setFormFieldName('slide');

        $this->getMassactionBlock()->addItem(
            'delete',
            [
                'label' => __('Delete'),
                'url' => $this->getUrl('slider/*/massDelete'),
                'confirm' => __('Are you sure?')
            ]
        );

        $statuses = $this->_status->getOptionArray();

        $this->getMassactionBlock()->addItem(
            'status',
            [
                'label' => __('Change status'),
                'url' => $this->getUrl('slider/*/massStatus', ['_current' => true]),
                'additional' => [
                    'visibility' => [
                        'name' => 'status',
                        'type' => 'select',
                        'class' => 'required-entry',
                        'label' => __('Status'),
                        'values' => $statuses
                    ]
                ]
            ]
        );


        return $this;
    }


    /**
     * @return string
     */

    protected function _filterStoreCondition($collection, \Magento\Framework\DataObject $column)
    {
        if (!($value = $column->getFilter()->getValue())) {
            return;
        }

        $this->getCollection()->addStoreFilter($value);
    }

    /**
     * @return string
     */
    public function getGridUrl()
    {
        return $this->getUrl('slider/*/index', ['_current' => true]);
    }

    /**
     * @param \Custom\Slider\Model\slide|\Magento\Framework\Object $row
     * @return string
     */
    public function getRowUrl($row)
    {

        return $this->getUrl(
            'slider/*/edit',
            ['slide_id' => $row->getId()]
        );

    }


    static public function getOptionArray2()
    {
        $data_array=array(); 
        $data_array[0]='Disabled';
        $data_array[1]='Enabled';
        return($data_array);
    }
    static public function getValueArray2()
    {
        $data_array=array();
        foreach(\Custom\Slider\Block\Adminhtml\Slide\Grid::getOptionArray2() as $k=>$v){
           $data_array[]=array('value'=>$k,'label'=>$v);        
        }
        return($data_array);

    }

    static public function getOptionArray8()
    {
        $data_array=array(); 
        $data_array['normal']='Normal';
        $data_array['hotspot']='Hotspot';
        $data_array['video']='Video';
        return($data_array);
    }
    static public function getValueArray8()
    {
        $data_array=array();
        foreach(\Custom\Slider\Block\Adminhtml\Slide\Grid::getOptionArray8() as $k=>$v){
           $data_array[]=array('value'=>$k,'label'=>$v);        
        }
        return($data_array);

    }


}
Was it helpful?

Solution

I have resolved this issue ( Show multi-store view on grid) by using the renderer.

Custom\Slider\Block\Adminhtml\Slide\Grid.php

$this->addColumn(
                    'store_ids',
                    [
                        'header' => __('Store Views'),
                        'index' => 'store_ids',                        
                        'type' => 'store',
                        'store_all' => true,
                        'store_view' => true,
                        'renderer'=>  'Custom\Slider\Block\Adminhtml\Slide\Edit\Tab\Renderer\Store',
                        'filter_condition_callback' => [$this, '_filterStoreCondition']
                    ]
                );

Custom\Slider\Block\Adminhtml\Slide\Edit\Tab\Renderer\Store.php

<?php

namespace Custom\Slider\Block\Adminhtml\Slide\Edit\Tab\Renderer; 

class Store extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer
{
    /**
     * @var bool
     */
    protected $_skipAllStoresLabel = false;

    /**
     * @var bool
     */
    protected $_skipEmptyStoresLabel = false;

    /**
     * @var \Magento\Store\Model\System\Store
     */
    protected $_systemStore;

    /**
     * @param \Magento\Backend\Block\Context $context
     * @param \Magento\Store\Model\System\Store $systemStore
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Context $context,
        \Magento\Store\Model\System\Store $systemStore,
        array $data = []
    ) {
        $this->_systemStore = $systemStore;
        parent::__construct($context, $data);
    }

    /**
     * Retrieve System Store model
     *
     * @return \Magento\Store\Model\System\Store
     */
    protected function _getStoreModel()
    {
        return $this->_systemStore;
    }

    /**
     * Retrieve 'show all stores label' flag
     *
     * @return bool
     * @SuppressWarnings(PHPMD.BooleanGetMethodName)
     */
    protected function _getShowAllStoresLabelFlag()
    {
        return $this->getColumn()->getData(
            'skipAllStoresLabel'
        ) ? $this->getColumn()->getData(
            'skipAllStoresLabel'
        ) : $this->_skipAllStoresLabel;
    }

    /**
     * Retrieve 'show empty stores label' flag
     *
     * @return bool
     * @SuppressWarnings(PHPMD.BooleanGetMethodName)
     */
    protected function _getShowEmptyStoresLabelFlag()
    {
        return $this->getColumn()->getData(
            'skipEmptyStoresLabel'
        ) ? $this->getColumn()->getData(
            'skipEmptyStoresLabel'
        ) : $this->_skipEmptyStoresLabel;
    }

    /**
     * Render row store views
     *
     * @param \Magento\Framework\DataObject $row
     * @return \Magento\Framework\Phrase|string
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @SuppressWarnings(PHPMD.NPathComplexity)
     */
    public function render(\Magento\Framework\DataObject $row)
    {
        $out = '';
        $skipAllStoresLabel = $this->_getShowAllStoresLabelFlag();
        $skipEmptyStoresLabel = $this->_getShowEmptyStoresLabelFlag();
        // $origStores =$row->getData($this->getColumn()->getIndex());
        $origStores = explode(',',$row->getData($this->getColumn()->getIndex()));

        if ($origStores === null && $row->getStoreName()) {
            $scopes = [];
            foreach (explode("\n", $row->getStoreName()) as $k => $label) {
                $scopes[] = str_repeat('&nbsp;', $k * 3) . $label;
            }
            $out .= implode('<br/>', $scopes) . __(' [deleted]');
            return $out;
        }

        if (empty($origStores) && !$skipEmptyStoresLabel) {
            return '';
        }
        if (!is_array($origStores)) {
            $origStores = [$origStores];
        }

        if (empty($origStores)) {
            return '';
        } elseif (in_array(0, $origStores) && count($origStores) == 1 && !$skipAllStoresLabel) {
            return __('All Store Views');
        }

        $data = $this->_getStoreModel()->getStoresStructure(false, $origStores);

        foreach ($data as $website) {
            $out .= $website['label'] . '<br/>';
            foreach ($website['children'] as $group) {
                $out .= str_repeat('&nbsp;', 3) . $group['label'] . '<br/>';
                foreach ($group['children'] as $store) {
                    $out .= str_repeat('&nbsp;', 6) . $store['label'] . '<br/>';
                }
            }
        }

        return $out;
    }

    /**
     * Render row store views for export
     *
     * @param \Magento\Framework\DataObject $row
     * @return \Magento\Framework\Phrase|string
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     */
    public function renderExport(\Magento\Framework\DataObject $row)
    {
        $out = '';
        $skipAllStoresLabel = $this->_getShowAllStoresLabelFlag();
        // $origStores = $row->getData($this->getColumn()->getIndex());
        $origStores = explode(',',$row->getData($this->getColumn()->getIndex()));

        if ($origStores === null && $row->getStoreName()) {
            $scopes = [];
            foreach (explode("\n", $row->getStoreName()) as $k => $label) {
                $scopes[] = str_repeat(' ', $k * 3) . $label;
            }
            $out .= implode("\r\n", $scopes) . __(' [deleted]');
            return $out;
        }

        if (!is_array($origStores)) {
            $origStores = [$origStores];
        }

        if (in_array(0, $origStores) && !$skipAllStoresLabel) {
            return __('All Store Views');
        }

        $data = $this->_getStoreModel()->getStoresStructure(false, $origStores);

        foreach ($data as $website) {
            $out .= $website['label'] . "\r\n";
            foreach ($website['children'] as $group) {
                $out .= str_repeat(' ', 3) . $group['label'] . "\r\n";
                foreach ($group['children'] as $store) {
                    $out .= str_repeat(' ', 6) . $store['label'] . "\r\n";
                }
            }
        }

        return $out;
    }
}

Output :

enter image description here

For filter

I have added _filterStoreCondition method as defined in callback filter_condition_callback.

protected function _filterStoreCondition($collection, $column){

         if (!$value = $column->getFilter()->getValue()) {
            return;
        }

        $this->getCollection()->addFieldToFilter('store_ids', array('finset' => $value));
    }

Source : A2bizz blog

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