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.
2 Answers 2
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...
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_beforecontroller_action_layout_generate_blocks_before
Edit:
Try this one: core_block_abstract_prepare_layout_before
-
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– Marco Antonino2017年11月17日 16:54:57 +00:00Commented Nov 17, 2017 at 16:54 -
Or, he don't find a breadcrumbs block, please try the footer
$layout->getBlock('footer'), try also this eventcore_block_abstract_prepare_layout_before2017年11月17日 17:07:22 +00:00Commented Nov 17, 2017 at 17:07 -
With those changes I've got the following error:
Fatal error: Maximum function nesting level of '256' reached, aborting!Marco Antonino– Marco Antonino2017年11月17日 17:35:06 +00:00Commented Nov 17, 2017 at 17:35
Explore related questions
See similar questions with these tags.
breadcrumbsblock is there on the page as you are appending your block to itbreadcrumbsblock is on the page because if I writevar_dump(Mage::app()->getLayout()), when I reload the page, in theprotected '_blocks'array there is the following element'breadcrumbs' => object(Mage_Page_Block_Html_Breadcrumbs)[199]