Hello Readers,  
In this tutorial, I will explain how to add Store View selection to Module’s Adminhtml
This article assumes you already know how to create Magento Admin grid.
Go to Namespace/Module/Block/Adminhtml/Module/Edit/Tab/Form.php
Add below code in _prepareForm() function.
   if (!Mage::app()->isSingleStoreMode()) {
            $fieldset->addField('stores', 'multiselect', array(
                    'name'      => 'stores[]',
                    'label'     => Mage::helper('slideshow')->__('Select Store'),
                    'title'     => Mage::helper('slideshow')->__('Select Store'),
                    'required'  => true,
                    'values'    => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(true, true),
                ));
            }
            else {
                $fieldset->addField('stores', 'hidden', array(
                    'name'      => 'stores[]',
                    'value'     => Mage::app()->getStore(true)->getId()
                ));
            }
Above Code show store view multiselect option to your form. This will work, if you have multiple stores in you setup.
Next, Go to Namespace/Module/Adminhtml/IndexController.php
Add below code in saveAction() function.
            if(isset($postData['stores']))
                {
                    if(in_array('0',$postData['stores'])){
                        $postData['stores'] = '0';
                    }
                    else{
                        $postData['stores'] = implode(",", $postData['stores']);
                    }
                    //unset($postData['stores']);
                }
                
                if($postData['stores'] == "")
                {
                    $postData['stores'] = '0';
                }
The above code will set stores to 0 if “All Store Views” was selected or sets stores as comma-seperated values using implode function.
Next, In your Custom modules Grid file (Path: Namespace/Module/Block/Adminhtml/Module/Grid.php).
Add the following code.
    protected function _prepareCollection()
        {
            $collection = Mage::getModel('slideshow/slideshow')->getCollection();
         // echo '<pre>'; print_r($collection->getData()); die;
                foreach($collection as $link){
                if($link->getStores() && $link->getStores() != 0 ){
                     $link->setStores(explode(',',$link->getStores()));
                }
                else{
                        $link->setStores(array('0'));
                }
            }
         
            $this->setCollection($collection);
        return parent::_prepareCollection();
        }
setStores(array('0')). Here '0' represents All Store Views.
In the same class file (i.e Grid.php), add the following code in prepareColumns() function
if (!Mage::app()->isSingleStoreMode()) {
    $this->addColumn('stores', array(
        'header'        => Mage::helper('')->__('Store View'),
        'index'         => 'stores',
        'type'          => 'store',
        'store_all'     => true,
        'store_view'    => true,
        'sortable'      => true,
        'filter_condition_callback' => array($this,
            '_filterStoreCondition'),
    ));
}
& add the following function in Grid.php:
protected function _filterStoreCondition($collection, $column){
    if (!$value = $column->getFilter()->getValue()) {
        return;
    }
    $this->getCollection()->addStoreFilter($value);
}
_filterStoreCondition($collection, $column): This function checks store filter has been selected or not and if so , then it will call add the filter function to the collection.
Lastly, Go to Collection file (Path: Namespace_Module_Model_Mysql4_Module_Collection) & add this function in collection.php
public function addStoreFilter($store, $withAdmin = true){
    if ($store instanceof Mage_Core_Model_Store) {
        $store = array($store->getId());
    }
    if (!is_array($store)) {
        $store = array($store);
    }
    $this->addFilter('stores', array('in' => $store));
    return $this;
}
                       
                    
1 Comment(s)