Magento 1.9 Custom Module – Fixing Grid Container Not Showing

gridgrid layoutmagento-1.9PHP

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

enter image description here

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>
            <adminhtml>
                <args>
                    <modules>
                        <company_custombundle before="Mage_Adminhtml">Company_CustomBundle_Adminhtml</company_custombundle>
                    </modules>
                </args>
            </adminhtml>
        </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>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>
    </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="custombundle/adminhtml_custombundle_bundle" name="list_combination" />
        </reference>
    </adminhtml_custombundle_index>
</layout>

controllers/Adminhtml/CustombundleController.php

public function indexAction()
{
    $this->loadLayout();
    $this->_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_bundle';
        $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()));
    }
}

Best Answer

In a quick look, I think there is something wrong with the protected property value $_controller of your grid container. So try to use this.

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('company_custombundle')->__('Custom Bundle Category combinations');
        $this->_addButtonLabel = Mage::helper('company_custombundle')->__('Add Item');
        parent::__construct();
    }
}

The only change here is, adminhtml_custombundle is changed to adminhtml_custombundle_bundle. This value is used to effectively identify grid class. As per definition of your classes, that tail part (ie _bundle) is also essential to correctly point towards your grid class. (ie Company_CustomBundle_Block_Adminhtml_Custombundle_Bundle_Grid)

EDIT - 1

I think your router definition is also wrong . you need to correct that in your config.xml file like this

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

The definition that you are using is deprecated from Magento 1.9 version. It is also not recommended to use.

There is also changes needed in the menu definition section as per above router change.

Try to use this:

<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>

Changes here are inside action node.

Edit - 2

Please keep the grid container change also in your code. It is essential. If it is not working, then try to simplify your indexAction() method in your controller like this.

public function indexAction()
{
    $this->loadLayout();
    $this->_setActiveMenu('custombundle/index');
    $this->renderLayout();
}

After this changes are made, do not forget to clear your cache before test it.

Related Topic