2

I am getting an error

Call to a member function append()....Mage/Adminhtml/Controller/Action.php on line 122 . I have worked out it is down to my indexAction in the controller

public function indexAction()
{
    $this->_addContent($this->getLayout()->createBlock('prefs/adminhtml_forms'));
    $this->renderLayout();
}

So I have taken a look at the origin of the error

protected function _addContent(Mage_Core_Block_Abstract $block)
{
    $this->getLayout()->getBlock('content')->append($block);
    return $this;
}

I see it requires a block that has to be Mage_Core_Block_Abstract and the block I am calling is Mage_Adminhtml_block_Widget_Grid_Container, however digging down I find Mage_Core_Block_Abstract is the being extended.

enter image description here

So I'm now left with the question, what have I done wrong and how do I resolve it?

class Ps_Prefs_Block_Adminhtml_Forms
extends Mage_Adminhtml_block_Widget_Grid_Container
{
public function _construct()
{
    $this->_controller = 'adminhtml_forms';//path to block
    $this->_blockGroup = 'prefs';//module name
    $this->_headerText = $this->__( 'Form Fields' );
    $this->_addButtonLabel = $this->__( 'New Field' );
    parent::_construct();
}

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

1 Answer 1

5

You need to add this line

$this->loadLayout();

before

$this->_addContent($this->getLayout()->createBlock('prefs/adminhtml_forms'));

In your case the layout is not loaded and there is no block with the name content. That's why

$this->getLayout()->getBlock('content')

returns null and you cannot call null->append().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.