Search code examples
phpmagentomagento-1.9

Custom module grid container will not show


I know this question has been asked for so many times here on SO but nothing helped me to solve my issue.

I am trying to display a grid on my modules index page but it is not showing, I tried to var_dump Mage::getModel('custombundle/bundle')->getCollection() in a loop and it gave me output of data. Below is what I have coded so far:

config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Company_CustomBundle>
            <version>1.0.1</version>
        </Company_CustomBundle>
    </modules>
    <admin>
        <routers>
            <custombundle>
                <use>admin</use>
                <args>
                    <module>Company_CustomBundle</module>
                    <frontName>admin_custombundle</frontName>
                </args>
            </custombundle>
        </routers>
    </admin>

    <global>
        <helpers>
            <custombundle>
                <class>Company_CustomBundle_Helper</class>
            </custombundle>
        </helpers>

        <!-- Blocks -->
        <blocks>
            <company_custombundle>
                <class>Company_CustomBundle_Block</class>
            </company_custombundle>
        </blocks>

        <models>
            <custombundle>
                <class>Company_CustomBundle_Model</class>
                <resourceModel>custombundle_resource</resourceModel>
            </custombundle>
            <custombundle_resource>

                <class>Company_CustomBundle_Model_Resource</class>

                <entities>
                    <basket>
                        <table>custombundle_basket</table>
                    </basket>
                    <bundle>
                        <table>custombundle_bundle</table>
                    </bundle>
                </entities>

            </custombundle_resource>
        </models>

        <resources>

            <custombundle_setup>
                <setup>
                    <module>Company_CustomBundle</module>
                    <class>Company_CustomBundle_Model_Resource_Setup</class>
                </setup>
            </custombundle_setup>

            <custombundle_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </custombundle_write>

            <custombundle_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </custombundle_read>

        </resources>

    </global>

    <adminhtml>

        <!-- Layouts Configuration Starts -->
        <layout>
            <updates>
                <custombundle>
                    <file>custombundle.xml</file>
                </custombundle>
            </updates>
        </layout>
        <!-- !! Layouts Configuration -->

        <menu>
            <custombundle module="custombundle">
                <title>Custom Bundle</title>
                <sort_order>100</sort_order>
                <children>
                    <index module="custombundle">
                        <title>Custom Bundle</title>
                        <sort_order>0</sort_order>
                        <action>admin_custombundle/adminhtml_custombundle</action>
                    </index>

                    <other module="custombundle">
                        <title>Other</title>
                        <sort_order>0</sort_order>
                        <action>admin_custombundle/adminhtml_custombundle/other</action>
                    </other>
                </children>
            </custombundle>
        </menu>
    </adminhtml>

</config>

app/design/adminhtml/default/default/layout/custombundle.xml

<?xml version="1.0"?>

<layout version="0.1.0">

    <adminhtml_custombundle_index> <!-- custombundle controller index action -->
        <reference name="content">
            <block type="company_custombundle/adminhtml_custombundle_bundle" name="list_combination" />
        </reference>
    </adminhtml_custombundle_index>
</layout>

controllers/Adminhtml/CustombundleController.php

public function indexAction()
{
    $this->_title($this->__('Custom Bundle'))->_title($this->__('Category Combinations'))->loadLayout()->_setActiveMenu('custombundle/index');

    $this->renderLayout();
}

Block/Adminhtml/Custombundle/Bundle.php

class Company_CustomBundle_Block_Adminhtml_Custombundle_Bundle extends Mage_Adminhtml_Block_Widget_Grid_Container
{
    public function __construct()
    {
        $this->_controller = 'adminhtml_custombundle';
        $this->_blockGroup = 'company_custombundle';
        $this->_headerText = Mage::helper('company_custombundle')->__('Custom Bundle Category combinations');
        $this->_addButtonLabel = Mage::helper('company_custombundle')->__('Add Item');
        parent::__construct();
    }
}

Block/Adminhtml/Custombundle/Bundle/Grid.php

class Company_CustomBundle_Block_Adminhtml_Custombundle_Bundle_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct()
    {
        parent::__construct();
        $this->setId('bundleGrid');
        $this->setDefaultSort('bundle_id');
        $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);
    }

    protected function _prepareCollection()
    {
        $collection = Mage::getModel('custombundle/bundle')->getCollection();
        $this->setCollection($collection);

        return parent::_prepareCollection();
    }

    protected function _prepareColumns()
    {
        $this->addColumn('bundle_id', array(
                'header' => 'ID',
                'align' => 'right',
                'width' => '50px',
                'index' => 'bundle_id',
        ));

        $this->addColumn('assigned_category_id', array(
              'header' => 'Assigned with',
              'align' => 'left',
              'index' => 'assigned_category_id',
        ));

        $this->addColumn('category_id', array(
              'header' => 'Category',
              'align' => 'left',
              'index' => 'category_id',
        ));

        return parent::_prepareColumns();
    }

    public function getRowUrl($row)
    {
         return $this->getUrl('*/*/edit', array('id' => $row->getId()));
    }
}

Solution

  • This question is answered properly on Magento stackexchange by Rajeev and Jaimin you can check the answer as well as discussion here. These guys really provided valuable inputs and suggested improvements that improved the quality of the code.

    I will try to explain a bit about the corrections I made in my code that solved my issue.

    config.xml

    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <company_custombundle before="Mage_Adminhtml">Company_CustomBundle_Adminhtml</company_custombundle>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
    

    In config.xml part I changed router part as <use>admin</use> is deprecated as of 1.9 version and had potential security threats as mentioned by Mladen Ilić.

    Then accordingly corrected the menu part of the config.xml to support new routing as below:

    <menu>
        <custombundle module="custombundle">
            <title>Custom Bundle</title>
            <sort_order>100</sort_order>
            <children>
                <index module="custombundle">
                    <title>Custom Bundle</title>
                    <sort_order>0</sort_order>
                    <action>adminhtml/custombundle/index</action>
                </index>
    
                <other module="custombundle">
                    <title>Other</title>
                    <sort_order>0</sort_order>
                    <action>adminhtml/custombundle/other</action>
                </other>
            </children>
        </custombundle>
    </menu>
    

    Block/Adminhtml/Custombundle/Bundle.php

    class Company_CustomBundle_Block_Adminhtml_Custombundle_Bundle extends Mage_Adminhtml_Block_Widget_Grid_Container
    {
        public function __construct()
        {
            $this->_controller = 'adminhtml_custombundle_bundle';
            $this->_blockGroup = 'company_custombundle';
            $this->_headerText = Mage::helper('custombundle')->__('Custom Bundle Category combinations');
            $this->_addButtonLabel = Mage::helper('custombundle')->__('Add Item');
            parent::__construct();
        }
    }
    

    Now here I changed adminhtml_custombundle to adminhtml_custombundle_bundle this change was done because if you look at Mage_Adminhtml_Block_Widget_Container::_prepareLayout() method you will get to know that your container's child i.e grid or Block/Adminhtml/Custombundle/Bundle/Grid.php will never be called if you don't do this.

    Another change in the above code was Mage::Helper() I changed its value from company_custombundle to custombundle only i.e without namespace.

    After all I cleared magento's cache and reload... *Bingo the grid was showing there with data.