0

I have created a custom backend module which contains four fields namely name, image, date, and store view selector. Everything is working fine except store view selector, I have used a renderer to display the selected store view in Grid.
If the store is selected individually it shows correctly if All Store view option is selected it is not showing in any store during filtering. Filtering is not working for All Store View option kindly help me out to solve this issue !!

This is my renderer to show store details in the grid

<?php
 class Hm_Blog_Block_Adminhtml_Blog_Renderer_Store extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row) 
{   
$stores = $row->getStoreId();
if($stores == 0){
    $store_details = Mage::app()->getStores();
     foreach($store_details as $astore)
        {
            $websiteId = $astore->getWebsiteId();
            $web = Mage::app()->getWebsite($websiteId)->getName();  
            echo  $web ."<br>";     
            $data = $astore->getGroupId();
            echo $data = '&nbsp;&nbsp;&nbsp;'. Mage::app()->getGroup($data)->getName().'<br />';
            echo $data ='&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$astore->getName().'<br />';
        }
    }
    else{
        $ids = explode(',', $stores);
        foreach($ids as $storeID){
                //Website details
            $store = Mage::getModel('core/website')->load($storeID)->getWebsiteId();
            $website_details = Mage::app()->getWebsite($store);
            $web = $website_details->getName(); 
            echo '&nbsp;&nbsp;&nbsp;'. $web ."<br>";         
                //Store_view details
            $store = Mage::getModel('core/store')->load($storeID);
            $store_view =$store->getGroupId();
            $store_details = Mage::app()->getGroup($store_view);
            $store_view_name = $store_details->getName();   
            echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'. $store_view_name."<br>" ;
            $name= $store->getName();?>
            <?php //echo $store_view_name ?>
            <?php echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'. $name ?><br>
    <?php }         
    }

}
}
?>

This is my Grid.php

<?php
 class Hm_Blog_Block_Adminhtml_Blog_Grid extends Mage_Adminhtml_Block_Widget_Grid
{   
public function __construct()
{
    parent::__construct();
    $this->setId('blogGrid');
    $this->setDefaultSort('blog_id');
    $this->setDefaultDir('ASC');
    $this->setSaveParametersInSession(true);
    $this->setUseAjax(true);

}

/*public function initForm()
{
    return $this;
}*/

protected function _prepareCollection()
{   

    $collection = Mage::getModel('blog/blog')->getCollection();

    /*foreach($collection as $view){
        if($view->getStoreId() && $view->getStoreId() != 0 ){
            $view->setStoreId(explode(',',$view->getStoreId()));
        }
        else{
            $view->setStoreId(array('0'));
        }
    }*/
    $this->setCollection($collection);
    return parent::_prepareCollection();

}

protected function _prepareColumns()
{

    $profile = Mage::getModel('blog/blog');

    $this->addColumn('blog_id', array(
      'header'    => Mage::helper('hm_blog')->__('Blog ID'),
      'align'     =>'right',
      'width'     => '50px',
      'sortable'  => true,
      'index'     => 'blog_id',
    ));

    $this->addColumn('blog_name', array(
      'header'    =>  Mage::helper('hm_blog')->__('Blog Name'),
      'align'     =>'left',
      'index'     => 'blog_name',
     //'width'   => '200px',
    ));

    $this->addColumn('blog_image', array(
        'header'    =>  Mage::helper('hm_blog')->__('Blog Image'),
        'width'     => '10px',
        'index'     => 'blog_image',
        'renderer'  => 'hm_blog/adminhtml_blog_renderer_image',
    ));

     $this->addColumn('published_date', array(
        'type'      => 'datetime', 
        'header'    => Mage::helper('hm_blog')->__('Published Date'),
       //'width'    => '100px',
       'index'      => 'published_date',
    ));
    if (!Mage::app()->isSingleStoreMode()) {
        $field = $this->addColumn('store_id', array(
        'type'      => 'store', 
        'header'    => Mage::helper('hm_blog')->__('Store View'),
      //'width'     => '100px',
        'index'     => 'store_id',
        'store_all' => true,
        'sortable'  => false,
        'store_view'=> true,
        'renderer'  => 'Hm_Blog_Block_Adminhtml_Blog_Renderer_Store',
        //'renderer'  => 'adminhtml/store_switcher_form_renderer_fieldset_element',
        'filter_condition_callback' => array($this,'_filterStoreCondition'),
        ));
        //$renderer = $this->getLayout()->createBlock('adminhtml/store_switcher_form_renderer_fieldset_element');
        //$field->setRenderer($renderer);
    }

    return parent::_prepareColumns();
}

 protected function _afterLoadCollection()
{
    $this->getCollection()->walk('afterLoad');
    parent::_afterLoadCollection();
}


protected function _filterStoreCondition($collection, $column)
{
    if (!$value = $column->getFilter()->getValue()){
        return $this;
    }
     $store = Mage::app()->getWebsite($value);
   // $collection->addStoreFilter($value);

    return $this->getCollection()->addFieldToFilter('store_id', array('finset' => $value));
    //return $this->getCollection()->addStoreFilter($value);
}


public function getRowUrl($row)
{
    return $this->getUrl('*/*/edit', array('id' => $row->getId()));
}
 public function getGridUrl()
{
  return $this->getUrl('*/*/grid', array('_current'=>true));
}
}                                                                 
3
  • why you use renderer ?? Commented Feb 22, 2018 at 6:19
  • Actually if i multiselect stores, options are not dispyed in grid .It shows empty field in grid.So i made a renderer and displayed based on stored id selected.
    – aravind
    Commented Feb 22, 2018 at 6:21
  • store_id in your grid,is not a single store_id? Commented Feb 22, 2018 at 8:26

2 Answers 2

1

Use the below function instead of your _filterStoreCondition($collection, $column) function.

protected function _filterStoreCondition($collection, $column)
{
    if (!$value = $column->getFilter()->getValue()){
        return $this;
    }
    $_array =explode(',',$value);
    foreach($_array as $arr)
    {
        $_filter[]['finset']= array($arr);
    }
    return $this->getCollection()->addAttributeToFilter('store_id', $_filter);
}

It will help you for filter values.

1
  • It is still not working..If I select Two stores It is displaying in both store perfectly. But if I select "All Store view" option it is not showing up in any of the store during filtering in grid.Any how thanks for your response..
    – aravind
    Commented Feb 22, 2018 at 9:47
0

I made it working by following code

 protected function _filterStoreCondition($collection, $column){
    $array = explode(",",$column->getFilter()->getValue());
    if(is_array($array)){
        foreach($array as $child){
            if (!$value = $child) {
                return;
            }
             $collection->addFieldToFilter('store_id',array(
                        array('finset'=> array('0')),
                        array('finset'=> array($value)),
                    ));
            return $this;
        }
    }
    else{
        if (!$value = $column->getFilter()->getValue()) {
            return;
        }
        $collection->addFieldToFilter('store_id',array(
                    array('finset'=> array('0')),
                    array('finset'=> array($value)),
                ));
        return $this;
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.