Magento – Fatal error Call to a member function insert() on boolean:Magento

event-observerfatal errorlayoutmagento-1module

I am facing an error during the checkout process when I select a payment method and press the button continue to next step to place order its an error as follows:

Fatal error Call to a member function insert() on boolean in /home/buildi59/public_html/app/code/community/Lof/Slideshow/Model/Observer.php on line 58

Here is the code for observer.php:

class Lof_Slideshow_Model_Observer
{

    public function beforeRender(Varien_Event_Observer $observer)
    {
        $controller_name = Mage::app()->getRequest()->getControllerModule();
        $menu_name = $controller_name . "_" . Mage::app()->getRequest()->getControllerName();
        $helper = Mage::helper('lof_slideshow/data');

        if ($helper->checkAvaiable($controller_name)) {
            $config = $helper->get();
            $event = $observer->getEvent();

            if ($helper->checkMenuItem($menu_name, $config)) {
                /*Define slider block*/
                $this->_loadMedia($config);
                $layout = Mage::getSingleton('core/layout'); // we are taking the item with 'object' key from array passed to dispatcher
                $title = $config["title"];
                $position = isset($config["blockPosition"]) ? $config["blockPosition"] : "content";
                $custom_pos = isset($config["customBlockPosition"]) ? $config["customBlockPosition"] : "";

                if (!empty($custom_pos)) {
                    $position = $custom_pos;
                }

                $display = isset($config["blockDisplay"]) ? $config["blockDisplay"] : "after";
                $display = $display == "after" ? true : false;
                $source = $config["source"];
                $block = $layout->createBlock('lof_slideshow/source_' . $source);
                // echo $source;die;
                $layout->getBlock($position)->insert($block, $title, $display);
                /*End defined*/
            } else {

            }
        }
    }

    function _loadMedia($config = [])
    {
        if (!defined('_LOADED_LOFSLIDESHOW_')) {
            $mediaHelper = Mage::helper('lof_slideshow/media');

            if ($config['enable_jquery']) {
                $mediaHelper->addMediaFile("js", "lof_slideshow/jquery.js");
            }

            $mediaHelper->addMediaFile("js", "lof_slideshow/jquery-ui.js");
            $mediaHelper->addMediaFile("js", "lof_slideshow/script.js");
            define('_LOADED_LOFSLIDESHOW_', 1);
        }
    }
}

Can any bobdy tell me how to fix this.

Thanks!

Best Answer

This is the line causing the problem:

$layout->getBlock( $position )->insert($block, $title , $display);

Basically the insert() does not work because $layout->getBlock( $position ) does not retrieve an existing block from the layout.

You need to find what $position is, where the corresponding block is declared in your layout and find out why it has been renamed/deleted.

If the fact that the block is not there is a normal behavior and you just want to skip the insert in this case then you can modify your code like this:

if ($parentBlock = $layout->getBlock($position)) {
    $parentBlock->insert($block, $title , $display);
}
Related Topic