简体   繁体   中英

Fatal error: Call to a member function toHtml() on a non-object

I have custom module jewellery. Tabs used in the admin form in category edit. Below code is used to add tabs in the admin form. it give fatal error

Fatal error: Call to a member function toHtml() on a non-object in D:\wamp\www\avita\app\code\local\Mage\Adminhtml\Block\Catalog\Category\Tabs.php on line 158

Below are my files

1.) local/Mage/Adminhtml/Block/Catalog/Category/Tabs.php

$this->addTab('upload_prices', array(
'label'     => Mage::helper('catalog')->__('Upload prices'),
'content'   => $this->getLayout()->createBlock('jewellery/adminhtml_catalog_category_product_tab')->toHtml(),
));

2.app/code/local/Subora/Jewellery/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Subora_Jewellery>
            <version>0.1.0</version>
        </Subora_Jewellery>
    </modules>
    <global>
        <models>
            <jewellery>
                <class>Subora_Jewellery_Model</class>
            </jewellery>
        </models>
        <helpers>
            <jewellery>
                <class>Subora_Jewellery_Helper</class>
            </jewellery>
        </helpers>
        <blocks>
            <jewellery>
                <class>Subora_Jewellery_Block</class>
            </jewellery>
        </blocks>
        <resources>
            <jewellery_setup>
                <setup>
                    <module>Subora_Jewellery</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </jewellery_setup>
            <jewellery_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </jewellery_write>
            <jewellery_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </jewellery_read>
        </resources>

    </global>
    <adminhtml>
        <layout>
            <updates>
                <jewellery>
                    <file>jewellery.xml</file>
                </jewellery>
            </updates>
        </layout>
        <events>
            <adminhtml_catalog_product_edit_prepare_form>
                <observers>
                    <jewellery_product_edit_prepare_form>
                        <class>jewellery/observer</class>
                        <method>productEditPrepareForm</method>
                    </jewellery_product_edit_prepare_form>
                </observers>
            </adminhtml_catalog_product_edit_prepare_form>
            <catalog_product_save_after>
                <observers>
                    <jewellery_save_product_data>
                        <type>singleton</type>
                        <class>jewellery/observer</class>
                        <method>saveProductTabData</method>
                    </jewellery_save_product_data>
                </observers>
            </catalog_product_save_after>
            <catalog_category_save_after>
                <observers>
                    <jewellery_save_category_data>
                        <type>singleton</type>
                        <class>jewellery/observer</class>
                        <method>saveCategoryTabData</method>
                    </jewellery_save_category_data>
                </observers>
            </catalog_category_save_after>
        </events>

    </adminhtml>

</config> 

3 app\\design\\frontend\\base\\default\\layout\\jewellery.xml

<?xml version="1.0"?>
<layout version="0.1.0">
  <jewellery_adminhtml_jewellerybackend_index>
    <reference name="content">
      <block type="jewellery/adminhtml_jewellerybackend" name="jewellerybackend"/>
    </reference>
  </jewellery_adminhtml_jewellerybackend_index>

    <jewellery_adminhtml_jewellerybackend_save>
        <reference name="content">
            <block type="jewellery/adminhtml_jewellerybackend_save" name="jewellerybackend_save" template="jewellery/jewellerybackend.phtml" />
        </reference>
    </jewellery_adminhtml_jewellerybackend_save>

    <adminhtml_catalog_product_edit>
        <reference name="product_tabs">
            <action method="addTab">
                <name>subora_jewellery_tab</name>
                <block>jewellery/adminhtml_catalog_product_tab</block>
            </action>
        </reference>
    </adminhtml_catalog_product_edit>

    <adminhtml_catalog_product_new>
        <reference name="product_tabs">
            <action method="addTab">
                <name>subora_jewellery_tab</name>
                <block>jewellery/adminhtml_catalog_product_tab</block>
            </action>
        </reference>
    </adminhtml_catalog_product_new>
</layout>

4.local\\Subora\\Jewellery\\Block\\Adminhtml\\Catalog\\Category\\Product\\Tab.php

<?php
class Subora_Jewellery_Block_Adminhtml_Catalog_Category_Product_Tab extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm() {
        $form = new Varien_Data_Form();

        $this->setForm($form);

        $fieldset = $form->addFieldset('category_import_prices', array('legend'=>Mage::helper('catalog')->__('Import prices')));
        $fieldset->addField('prices', 'file', array(
            'label' => Mage::helper('catalog')->__('Prices file (CSV only)'),
            'name'=> 'prices',
        ));

        return parent::_prepareForm();
    }
}

Now what to do i have tried but cant add tab??

The problem persists here

$this->getLayout()->createBlock('jewellery/adminhtml_catalog_category_product_tab')->toHtml();

toHtml() is calling in a non-object means, createBlock('jewellery/adminhtml_catalog_category_product_tab') is not working correctly. createBlock() is used to create a new block and add it to the layout. This function has 3 parameters.

  1. type 2. name 3. attributes

you have specified type as jewellery/adminhtml_catalog_category_product_tab . SO magento will look for app/code/local/Subora/Jewellery/Block/Adminhtml/Catalog/Category/Product/Tab.ph‌​p and the file should be properly declared.

Next, you need to specify a block name. It is necessary. Magento needs all blocks has unique names. Name is absent here.

Next parameter is attribute. It is optionnal. You can set a template by using this parameter. So you can try this code.

$this->getLayout()->createBlock(
    'jewellery/adminhtml_catalog_category_product_tab',
    'jewellery.tab',
     array('template' => 'your/template.phtml') //if any
)->toHtml();

EDIT

First you need to ensure, your module is active or not. You can do this via admin. System > Configuration > Advanced . Check your module is active there.

Next thing is, your layout file is in wrong postion. jewellery.xml is an admin layout file. It should reside in app\\design\\adminhtml\\default\\default\\layout\\jewellery.xml instead of app\\design\\frontend\\base\\default\\layout\\jewellery.xml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM