Call to a member function setSaveParametersInSession() on boolean

Error

You may encounter a Call to a member function setSaveParametersInSession() on boolean error in Magento when adding an adminhtml grid within a custom module.

Solution

This error occurs within the _prepareLayout() method of the Mage_Adminhtml_Block_Widget_Grid_Container class.

protected function _prepareLayout()
{
    $this->setChild( 'grid',
        $this->getLayout()->createBlock( $this->_blockGroup.'/' . $this->_controller . '_grid',
        $this->_controller . '.grid')->setSaveParametersInSession(true) );
    return parent::_prepareLayout();
}

Within this method, the argument passed into createBlock() is the resulting grid block class of your module, which is a result of the $this->_blockGroup and $this->_controller properties defined within your grid container class.

Within the grid container class, you may have the following code.

// app/code/local/[Vendor]/[Module]/Block/Adminhtml/Entity.php

<?php
class [Vendor]_[Module]_Block_Adminhtml_Entity extends Mage_Adminhtml_Block_Widget_Grid_Container 
{
    public function __construct()
    {
        parent::__construct();
        $this->_controller = 'adminhtml_entity';
        $this->_blockGroup = '[vendor]_[module]';
    }
}

The value of $this->_blockGroup should match the block prefix node defined in the module’s config.xml file.

// app/code/local/[Vendor]/[Module]/etc/config.xml

<?xml version="1.0"?>
<config>
    <global>
        <blocks>
            <[vendor]_[module]> <!-- This node should match $this->_blockGroup -->
                <class>[Vendor]_[Module]_Block</class>
            </[vendor]_[module]>
        </blocks>
    </global>
</config>

The value of $this->_controller should match the path after the block prefix defined in config.xml, which results in the full path to the grid container class.

The block prefix in this example is seen below.

// app/code/local/[Vendor]/[Module]/etc/config.xml

<?xml version="1.0"?>
<config>
    <global>
        <blocks>
            <[vendor]_[module]>
                <class>[Vendor]_[Module]_Block</class> <!-- This value is the block prefix -->
            </[vendor]_[module]>
        </blocks>
    </global>
</config>

The grid container class in this example is [Vendor]_[Module]_Block_Adminhtml_Entity, therefore the adminhtml_entity part is what is left to create the full path.

Therefore adminhtml_entity is the value needed for $this->_controller.

Ensuring that the values of the $this->_blockGroup and $this->_controller properties are correct will prevent this error from occurring.

Note: This article is based on Magento Open Source version 1.9.