1

I'm trying to add a static block to a page with an observer. The event that I'm listening to is controller_action_layout_generate_blocks_after and this is my observer code:

<?php
...
class Antonino_Banner_Model_Observer
{
    public function showBannerWhenLoad(Varien_Event_Observer $observer) 
    {
        $layout = Mage::app()->getLayout();
        $pageId = Mage::app()->getRequest()->getParam('id');
        $selectedCatId = Mage::getStoreConfig('antoninobanner/general/choose_category');

       if ($pageId==$selectedCatId){
           Mage::log('Bingo! PageId= '. $pageId.' SelectedId= '.$selectedCatId);
           $messageBlock = $layout->createBlock('cms/block')->setBlockId('antonino_banner');
           $breadcrumbs = $layout->getBlock('breadcrumbs');
           $breadcrumbs->append($messageBlock);
       }/*else{
           Mage::log('Oh no! The ids do not match');
       }*/
    }
}

The event is regularly triggered and in the system.log I can read the "Bingo!..." string. But on the page, the 'antonino_banner' block is not added. Naturally, I've created a static block in admin->static blocks.

Any help will be appreciated, thanks.

  • Make sure breadcrumbs block is there on the page as you are appending your block to it – Piyush Nov 17 '17 at 13:23
  • Thank you for your answer. Yes, the breadcrumbs block is on the page because if I write var_dump(Mage::app()->getLayout()), when I reload the page, in the protected '_blocks' array there is the following element 'breadcrumbs' => object(Mage_Page_Block_Html_Breadcrumbs)[199] – Marco Antonino Nov 17 '17 at 13:33
0

Ok, I think could handle this, but I'm not very satisfied by the result. I'm listening to

core_block_abstract_to_html_after

And my Observer code is:

<?php    

class Antonino_Banner_Model_Observer
{
    public function showBannerWhenLoad(Varien_Event_Observer $observer) {
        $pageId = Mage::app()->getRequest()->getParam('id');
        $selectedCatId = Mage::getStoreConfig('antoninobanner/general/choose_category');

       if ($pageId==$selectedCatId){
           $block = $observer->getBlock();
           if($block->getNameInLayout() == 'breadcrumbs')
           {
               $transport = $observer->getTransport();
               $html = $transport->getHtml();
               $html .= '<h1>We need to append something here</h1>';
               $transport->setHtml($html);
           }
       }/*else{
           Mage::log('Oh no! The ids do not match');
       }*/
    }
}

I don't like this solution very much because this means I must write some raw HTML code directly in this page...

0

I think that your are not using a good event, it's a little late and the layout is already rendered, try to use these :

  • controller_action_layout_load_before
  • controller_action_layout_generate_blocks_before

Edit:

Try this one: core_block_abstract_prepare_layout_before

Reference

  • Thank you for your answer. Unfortunatly both the events that you suggested appen too soon: the blocks are not still loaded. In fact I get this error: Call to a member function append() on boolean . I think this means that magento still doesn't know the name of the blocks. – Marco Antonino Nov 17 '17 at 16:54
  • Or, he don't find a breadcrumbs block, please try the footer $layout->getBlock('footer'), try also this event core_block_abstract_prepare_layout_before – PЯINCƏ Nov 17 '17 at 17:07
  • With those changes I've got the following error: Fatal error: Maximum function nesting level of '256' reached, aborting! – Marco Antonino Nov 17 '17 at 17:35

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.