Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento admin issue

Tags:

magento

Hi I'm creating my first admin section for my extension. I've created a menu which links to a page displaying a grid. The problem is when you click through to edit the record, its displaying this error

Fatal error: Call to a member function setData() on a non-object in /Applications/MAMP/htdocs/theBookClub/app/code/core/Mage/Adminhtml/Block/Widget/Form/Container.php on line 129

For the life of me I can't see any reference in any of the relevant files

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

    $this->_objectId = 'id';
    $this->_blockGroup = 'bookshelf';
    $this->_controller = 'bookshelf_admin';
    $this->_mode = 'edit';

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

    $this->_formScripts[] = "
        function toggleEditor() {
            if (tinyMCE.getInstanceById('form_content') == null) {
                tinyMCE.execCommand('mceAddControl', false, 'edit_form');
            } else {
                tinyMCE.execCommand('mceRemoveControl', false, 'edit_form');
            }
        }

        function saveAndContinueEdit(){
            editForm.submit($('edit_form').action+'back/edit/');
        }
    ";
}

public function getHeaderText()
{
    if (Mage::registry('example_data') && Mage::registry('example_data')->getId())
    {
        return Mage::helper('bookshelf')->__('Edit Example "%s"', $this->htmlEscape(Mage::registry('example_data')->getName()));
    } else {
        return Mage::helper('bookshelf')->__('New Example');
    }
}

and this

class Namespace_Bookshelf_Block_Adminhtml_Bookshelf_Grid extends                Mage_Adminhtml_Block_Widget_Grid {

public function __construct() {
    parent::__construct();
    $this->setId('bookshelf_grid');
    $this->setDefaultSort('bookshelf_id');
    $this->setDefaultDir('desc');
    $this->setSaveParametersInSession(true);
}

protected function _prepareCollection() {
    $collection = Mage::getModel('bookshelf/bookshelf')->getCollection();
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

protected function _prepareColumns() {
    $this->addColumn('bookshelf_id', array(
        'header' => Mage::helper('bookshelf')->__('ID'),
        'align' => 'right',
        'width' => '50px',
        'index' => 'bookshelf_id',
    ));

    $this->addColumn('customer_id', array(
        'header' => Mage::helper('bookshelf')->__('Name'),
        'align' => 'left',
        'index' => 'customer_id',
    ));

    $this->addColumn('bookshelf_name', array(
        'header' => Mage::helper('bookshelf')->__('Name'),
        'align' => 'left',
        'index' => 'bookshelf_name',
    ));



    return parent::_prepareColumns();
}





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

}

and this

class Newdaymedia_Bookshelf_Block_Adminhtml_Bookshelf extends
Mage_Adminhtml_Block_Widget_Grid_Container
{
public function __construct()
{
$this->_controller = 'bookshelf_admin';
$this->_blockGroup = 'bookshelf';
$this->_headerText = Mage::helper('bookshelf')->__('Item Manager');
$this->_addButtonLabel = Mage::helper('bookshelf')->__('Add Item');
parent::__construct();
}

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

Any help would be gratefully received!

NEW

class Namespace_Bookshelf_Block_Adminhtml_Bookshelf_Edit_Form extends     Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
    if (Mage::getSingleton('adminhtml/session')->getExampleData())
    {
        $data = Mage::getSingleton('adminhtml/session')->getExamplelData();
        Mage::getSingleton('adminhtml/session')->getExampleData(null);
    }
    elseif (Mage::registry('example_data'))
    {
        $data = Mage::registry('example_data')->getData();
    }
    else
    {
        $data = array();
    }
    Mage::log("this is the form class");
    $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);

    $fieldset = $form->addFieldset('example_form', array(
         'legend' =>Mage::helper('bookshelf')->__('Example Information')
    ));

    $fieldset->addField('name', 'text', array(
         'label'     => Mage::helper('bookshelf')->__('Name'),
         'class'     => 'required-entry',
         'required'  => true,
         'name'      => 'name',
         'note'     => Mage::helper('awesome')->__('The name of the example.'),
    ));

    $fieldset->addField('description', 'text', array(
         'label'     => Mage::helper('bookshelf')->__('Description'),
         'class'     => 'required-entry',
         'required'  => true,
         'name'      => 'description',
    ));

    $fieldset->addField('other', 'text', array(
         'label'     => Mage::helper('bookshelf')->__('Other'),
         'class'     => 'required-entry',
         'required'  => true,
         'name'      => 'other',
    ));

    $form->setValues($data);

    return parent::_prepareForm();
}

}

like image 528
jamessy Avatar asked Feb 20 '23 11:02

jamessy


2 Answers

This might solve your issue:

In your form container class:

Namespace_Bookshelf_Block_Adminhtml_Bookshelf_Edit 
extends Mage_Adminhtml_Block_Widget_Form_Container

If you add these test lines, you'll see which Form class Magento is trying to load

$form_block = $this->_blockGroup . '/' . 
              $this->_controller . '_' . 
              $this->_mode . 
              '_form';
echo $form_block;

For this to work with your code (above), your class:

Namespace_Bookshelf_Block_Adminhtml_Bookshelf_Edit_Form

should echo a test value of:

bookshelf/adminhtml_bookshelf_edit_form

and should be in the following filepath:

app/code/local/Namespace/Bookshelf/Block/Adminhtml/Bookshelf/Edit/Form.php

You might have to adjust the class name(s), or the filepath, or both, in order to get it working.

Good luck!

like image 130
Silas Palmer Avatar answered Feb 26 '23 23:02

Silas Palmer


You need to have a helper created for your module (even if it does nothing). In your module's helper directory create a file called Data.php and create a class in there called Namespace_Module_Helper_Data that extends Mage_Core_Helper_Abstract.

I think that should help clear the problem up.

like image 44
Josh Pennington Avatar answered Feb 27 '23 00:02

Josh Pennington