Magento 1.9 – Fix Recoverable Error in Adminhtml Controller

backendblocksmagento-1.9

I have a backend controller with an action :

public function newAction() {
    $this->loadLayout();
    $this->_addContent($this->getLayout()->createBlock('trackcode/adminhtml_trackcode_edit'))
         ->_addLeft($this->getLayout()->createBlock('trackcode/adminhtml_trackcode_edit_tabs'));
    $this->renderLayout();
}

Form container (app\code\local\Rayman\Trackcode\Block\Adminhtml\Trackcode\Edit.php):

class Rayman_Trackcode_Block_Adminhtml_Trackcode_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
    public function __construct()
    {
        parent::__construct();

        $this->_objectId = 'id';
        $this->_blockGroup = 'trackcode';
        $this->_controller = 'adminhtml_trackcode';

        $this->_updateButton('save', 'label', Mage::helper('rayman_trackcode')->__('Save'));
        $this->_updateButton('delete', 'label', Mage::helper('rayman_trackcode')->__('Delete'));

        $this->_addButton('saveandcontinue', array(
            'label'     => Mage::helper('adminhtml')->__('Save And Continue Edit'),
            'onclick'   => 'saveAndContinueEdit()',
            'class'     => 'save',
        ), -100);
    }

    public function getHeaderText()
    {
        return Mage::helper('rayman_trackcode')->__('My Form Container');
    }
}

left tab (app\code\local\Rayman\Trackcode\Block\Adminhtml\Trackcode\Edit\Tabs.php):

<?php
class Rayman_Trackcode_Block_Adminhtml_Trackcode_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs
{

  public function __construct()
  {
      parent::__construct();
      $this->setId('form_tabs');
      $this->setDestElementId('edit_form'); // this should be same as the form id define above
      $this->setTitle(Mage::helper('rayman_trackcode')->__('Product Information'));
  }

  protected function _beforeToHtml()
  {
      $this->addTab('form_section', array(
          'label'     => Mage::helper('rayman_trackcode')->__('Item Information'),
          'title'     => Mage::helper('rayman_trackcode')->__('Item Information'),
          'content'   => $this->getLayout()->createBlock('trackcode/adminhtml_form_edit_tab_form')->toHtml(),
      ));

      return parent::_beforeToHtml();
  }
}

Form(app\code\local\Rayman\Trackcode\Block\Adminhtml\Trackcode\Edit\Form.php)

class Rayman_Trackcode_Block_Adminhtml_Trackcode_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
  protected function _prepareForm()
  {
      $form = new Varien_Data_Form(array(
                                      'id' => 'edit_form',
                                      'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
                                      'method' => 'post',
                                      'enctype' => 'multipart/form-data'
                                   )
      );

      $form->setUseContainer(true);
      $this->setForm($form);
      return parent::_prepareForm();
  }
}

When i run the controller at menu, it produces an error

Recoverable Error: Argument 1 passed to Mage_Adminhtml_Controller_Action::_addContent() must be an instance of Mage_Core_Block_Abstract, boolean given

anyone know what is the problem?

—-update—–

<?xml version="1.0"?>
<config>
    <modules>
        <Rayman_Trackcode>
            <version>1.0.0</version>
        </Rayman_Trackcode>
    </modules>
    <global>
        <helpers>
            <rayman_trackcode>
                <!-- Helper definition needed by Magento -->
                <class>Mage_Core_Helper</class>
            </rayman_trackcode>
        </helpers>
        <blocks>
            <trackcode>
                <class>Rayman_Trackcode_Block</class>
            </trackcode>
        </blocks>       
        <models>
            <trackcode>
                <class>Rayman_Trackcode_Model</class>           
                <resourceModel>trackcode_mysql4</resourceModel>
            </trackcode>
            <trackcode_mysql4>
                <class>Rayman_Trackcode_Model_Mysql4</class>
                <entities>
                    <trackcode>
                        <table>rayman_trackcode</table>
                    </trackcode>
                </entities>
            </trackcode_mysql4>
        </models>
        <events>
            <salesrule_validator_process>
                <observers>
                    <rayman_trackcode>
                        <type>model</type>
                        <class>Rayman_Trackcode_Model_Observer</class>
                        <method>trackcode</method>
                    </rayman_trackcode>
                </observers>
            </salesrule_validator_process>
        </events>       
    </global>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <Rayman_Trackcode before="Mage_Adminhtml">Rayman_Trackcode_Adminhtml</Rayman_Trackcode>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
    <adminhtml>
        <layout>
            <updates>
                <trackcode>
                    <file>trackcode.xml</file>
                </trackcode>
            </updates>
        </layout>
    </adminhtml>
</config>

Form.php
(app\code\local\Rayman\Trackcode\Block\Adminhtml\Trackcode\Edit\Tab\Form.php)

<?php
class Rayman_Trackcode_Block_Adminhtml_Trackcode_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
        $form = new Varien_Data_Form();
        $this->setForm($form);
        $fieldset = $form->addFieldset('trackcode_form', array('legend'=>Mage::helper('rayman_trackcode')->__('Item information')));

        $fieldset->addField('title', 'text', array(
          'label'     => Mage::helper('rayman_trackcode')->__('Title'),
          'class'     => 'required-entry',
          'required'  => true,
          'name'      => 'title',
        ));

        return parent::_prepareForm();
    }
}

Best Answer

Based on the comments....

the initial problem comes from the fact that one of the createBlock calls in the newAction method returns false instead of an instance of a block.
This can come from different sources.

  • the blocks alias is not properly defined in config.xml
  • the block referenced does not exist.
  • the block does not extend Mage_Core_Block_Abstract.
Related Topic