0

I don't know why but i get this error: Call to a member function setData() on boolean in observer line 22, and this line is: $head->setData('title', $metaTitle);

Observer.php

class Namespace_Category_Model_Observer
{
    public function setMetaTitle(Varien_Event_Observer $observer)
    {
        $head = $observer->getLayout()->getBlock('head');
        $metaTitle = Mage::registry('current_category')->getMeta_title();
        Mage::log($metaTitle); //I get the right value
        if (Mage::registry('current_category')) {
            $head->setData('title', $metaTitle); //line 22
        }
    }
}

The event that i used is: <controller_action_layout_load_before>

EDIT: I just realized that i can't return a head block, $head return nothing !

I tryed also Mage::app()->getLayout()->getBlock('head') but it doesn't work

  • Make some debug on it... It seems like 'head' block it's not found on layout structure. Maybe you'll find some informations in '$observer->getLayout()' output. – Alex Constantinescu Oct 18 '17 at 12:06
  • effectively, i can't get my head block, $head return nothing – PЯINCƏ Oct 18 '17 at 12:36
  • That's what I'm talking about... your $head variable is null. Try to debug it step by step. First of all make sure that $observer->getLayout() is an object and it's not null. If it's not null, try to find some informations about block names included. You can render that output with a var_dump or by using xdebug. – Alex Constantinescu Oct 18 '17 at 13:08
  • yes i have do that but some data and not a head one – PЯINCƏ Oct 18 '17 at 13:25
3

Your getBlock('head') returns false, so setData() cant work.

Please try controller_action_layout_render_before_catalog_category_view event and an observer like ...

public function setMetaTitle(Varien_Event_Observer $observer)
{
    $category = Mage::registry('current_category');
    $head = Mage::app()->getLayout()->getBlock('head');
    if ($head) {
        $head->setData('title', 'new meta title');
        $category->setData('name', 'new category title');
    }
}
| improve this answer | |
  • It's doesn't work with this event @sv3n, it loads very long time and the category page dones't display – PЯINCƏ Oct 18 '17 at 13:41
  • @Prince Strange. Flushed chache after chaning event? Tested this on fresh magento. – sv3n Oct 18 '17 at 13:43
  • Yes, it's verry Strange, of course I flush a cache every time when i do some update – PЯINCƏ Oct 18 '17 at 13:46
  • Sorry @sv3n i don't know why but it works now, YES YES, thanks Can you tel me how you found this event and why it does't for my event, because i find this a litle strange, (to my knowledge the controller_action_layout_load_before loads all the layout before rendering) – PЯINCƏ Oct 18 '17 at 13:54
  • @Prince for finding events either add logging to dispatchEvent method in App.php or use pulsestorm.net/commerce-bug (worth its money!) Your layout_load_before events is triggered bit to early, you need one after load, but befor rendering. – sv3n Oct 18 '17 at 14:20
1

If you want to change layout using observer you can change your event to

controller_action_layout_generate_blocks_after

Then you can get your head block in observer method from below code

$controller   = $observer->getAction();
$layout       = $controller->getLayout();
$head = $layout->getBlock('head');
| improve this answer | |
  • the event is not a good one, he bug my browser – PЯINCƏ Oct 18 '17 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.