Magento 1.9 Admin Grid – How to Add Store View to Module

adminhtmlmagento-1.9

I ve been looking around for a good solution to display the store view in a column of my admin grid.

I have the following in my grid

class NsTest_Test_Block_Adminhtml_Testimonial_Grid extends Mage_Adminhtml_Block_Widget_Grid {
...

  protected function _prepareCollection()
  {
      $collection = Mage::getModel('test/testimonial')->getCollection();

    $this->setCollection($collection);
    return parent::_prepareCollection();
}


...

I read it is possible to add the store view with this code from some block on internet

$collection = Mage::getModel('tc_storeview/stores')->getResourceCollection();

foreach ($collection as $view) {
    if ( $view->getStoreId() && $view->getStoreId() != 0 ) {
        $view->setStoreId(explode(',',$view->getStoreId()));
    } else {
        $view->setStoreId(array('0'));
    }
}

But how to use this? I think the code would show the store view, but how about my data.
Is there a way to join both, store and my model ?


Seems like below code is working, still dont really understand how that can work.

class NsTest_Test_Block_Adminhtml_Testimonial_Grid extends Mage_Adminhtml_Block_Widget_Grid {
...

  protected function _prepareCollection()
  {
      $collection = Mage::getModel('test/testimonial')->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();
}

Best Answer

for store grind column use below code. store is database coulmn name

 $this->addColumn('store', array( 
                    'header' => 'Website', 
                    'index' => 'store', 
                    'type' => 'store', 
                    'width' => '100px', 
                    'store_view'=> true, 
                    'display_deleted' => false, 
                    'renderer' => 'Namespace_Modulename_Block_Adminhtml_Store',
                    )); 

for rendering the stores

<?php 

class Namespcae_Modulename_Block_Adminhtml_Store
    extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row) 
    {    $store=explode(',',$row->getStore());

            $data="";
         if($row->getStore()!="" and $row->getStore()==0)
         { 
            $allstore=Mage::app()->getStores();
            foreach($allstore as $astore)
            {
                $data.=$astore->getName().'<br />';
            }

             } else {
                $data="";
                $a=0;
                foreach ($store as $sto)
                {
                     $data= $data.Mage::getModel('core/store')->load($sto[$a])->getName().'<br>';
                 $a+1;}
             }

        return $data;
    }
} 
Related Topic