1

Let me preface with the fact that getLayout->createBlock->toHtml continue to mystify me.

There is a line of code that is throwing an error:

$this->getLayout()->createBlock('distributor/adminhtml_distributors_edit_tab_chat')->toHtml()

The result states that

Fatal error: Call to a member function toHtml() on a non-object in....
Doing a gettype() on `$this->getLayout()->createBlock('distributor/adminhtml_distributors_edit_tab_chat')

It shows that it's returning a boolean rather than an object.

I am trying to understand why this would return a boolean rather than the object that it is expecting.

I did search the error on Google, with the results stating to make sure that the block exists. As far as I can tell, it does. I was hoping it would be simple but never seems to be my luck.

The system is setup to run multiple sites, the sites that don't have database entries associated with this block allow the page to display correctly. Even after deleting the database entries, clearing the cache and retesting the page, the same error occurs.

Thank you for your time and assistance.

Update 01

Based on the replies, I am adding more (please let me know what else would be necessary to assist).

  • This is a code base that already exists.
  • This is code that had been running in production without incident.

The code below exists in app/code/local/codebase/Distributor/Block/Adminhtml/Distri‌​butors/Edit/Tabs.php

$this->addTab('distributor_chat_section', array(
    'label'     => Mage::helper('distributor')->__('Live Chat'),
    'title'     => Mage::helper('distributor')->__('Live Chat'),
    'content'   => $this->getLayout()->createBlock('distributor/adminhtml_distributors_edit_tab_chat')->toHtml()
        ));
5
  • where you are writing this code ? Commented Jun 6, 2017 at 4:12
  • I was not writing the code. It already exists and "seemed" to be working in the past.
    – oach
    Commented Jun 6, 2017 at 11:52
  • Could you add content of distributor/adminhtml_distributors_edit_tab_chat too? Commented Jun 6, 2017 at 23:36
  • Adarsh, the issue was in that file. In spite of $this->getLayout() returning the false, the issue was with the full $this->getLayout()->createBlock(). In ...Chat.php there is an api call in which the endpoint is returning a 301. Unfortunately a json_decode call was not wrapped in try/catch and the 301 return is not json. STILL, I don't understand how the $this->getLayout() returns false when the issue is with the creatBlock() call, order of operations doesn't make sense to me.
    – oach
    Commented Jun 8, 2017 at 11:16
  • I doubt you are not extending right class for your app/code/local/codebase/Distributor/Block/Adminhtml/Distri‌​butors/Edit/Tabs.php. Check this github.com/OpenMage/magento-mirror/blob/…, in this core core team has used very similar perspective. Check if you are extending this Mage_Adminhtml_Block_Widget_Tabs class. Also, please add full file code. Commented Jun 26, 2017 at 2:31

2 Answers 2

1

Problem looks like with the $this->getLayout() not returning the layout.

You may verify that with this:

Mage::log($this->getLayout(), null, 'my-log.log');

This will generate my-log.log file in var/log folder and you should be able to see what it is returning.

If you are calling that method in your controller then,

Like @Sohel has suggested you can load the layout first and then call it:

$this->loadLayout(); //this will load the layout
$this->getLayout()->createBlock('distributor/adminhtml_distributors_edit_tab_chat')->toHtml();

Hope this helps.

3
  • This is being called in another block. File structure is: app/code/local/<codebase>/Distributor/Block/Adminhtml/Distri‌​butors/Edit/Tabs.php and it makes a call to the block in question and contains the code that calls app/code/local/<codebase>/Distributor/Block/Adminhtml/Distri‌​butors/Edit/Tab/Chat‌​.php. Based on that, will $this->loadLayout() fail?
    – oach
    Commented Jun 6, 2017 at 12:11
  • Have you considered whitelisting your block: distributor/adminhtml_distributors_edit_tab_chat in Config > permission > block? Commented Jun 6, 2017 at 13:00
  • I added distributor/adminhtml_distributors_edit_tab_chat to the block permission as allowed. Didn't help, same error.
    – oach
    Commented Jun 6, 2017 at 14:14
0

Add following line before


$this->loadLayout();

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.