Mahmood Rehman
Mahmood Rehman

Reputation: 4331

How to inject new block in magento

I am working on custom module and try to embed my custom module block code on cms block during loading time. My code for block is:

{{block type="module/block_name" categories="2,4,3" template="catalog/product/list.phtml" }}

This runs on cms block page. But I am trying to embed the block code during run time. For this I create an event

 <events>
     <controller_action_layout_generate_blocks_before>
          <observers>
              <mypage>
                       <type>singleton</type>
                       <class>Module_Model_Observer</class>
                       <method>addlisttocms</method>
                </mypage>
         </observers>
     </controller_action_layout_generate_blocks_before>
</events> 

While on observer I create function :

public function addlisttocms(Varien_Event_Observer $observer)
    {
     $controller = Mage::app()->getRequest()->getControllerName();
     $layout = Mage::app()->getRequest()->getRouteName();
     $var = Mage::app()->getRequest()->getActionName();
     $page =  Mage::app()->getRequest()->getParam('page_id');
}

Now I want that when the cms page load, I embed my block using above function. Is it possible ?

Upvotes: 0

Views: 2333

Answers (2)

Mahmood Rehman
Mahmood Rehman

Reputation: 4331

I got my answer.change event to 'controller_action_layout_generate_xml_before' and in observer inject code like

  $layout = $observer->getLayout();
 $layout->getUpdate()->addUpdate('<reference name="content"> 
    <block name="mymodule" type="mymodule/blockname"  template="catalog/product/list.phtml">
        <action method="setCategories">
            <ids>2,3,4</ids>
        </action>
    </block>
</reference>');
$layout->generateXml();.

For refrence check the site.

Upvotes: 1

benmarks
benmarks

Reputation: 23205

Use layout XML or a custom widget to do this.

Custom module layout XML file:

<cms_page_view>
    <block name="some.block" type="module/block_name" parent="content" template="catalog/product/list.phtml">
        <action method="setCategories">
            <ids>2,3,4</ids>
        </action>
    </block>
</cms_page_view>

Upvotes: 2

Related Questions