Blocks are a way by which Magento distinguishes the array of functionalities in the system and creates a modular way to manage it from both visual and functional stand point. However, you can accurately hold the block lifecycle or events fired in a block? This post will attempt to give you more knowledge of those so you can change some actions of blocks to adjust your Magento site.

1. Stages in the lifecycle of a block

Block lifecycle starts when the block was generated and ends at the time it was destroyed.

a. Generate a block: Block was generated when:

  • The system loads layout and generates resemble blocks declared in layout file (includes implementing the modes when declaring layout – being called when action calls loadLayout() function)
<layout>
<adminhtml_sales_order_index>
<reference name="content">
<block type="adminhtml/sales_order" name="sales_order.grid.container" />
</reference>
</adminhtml_sales_order_index>
</layout>
  • The system calls a function to create a block (maybe in controllers/action or in a parent block)
public function indexAction(){
    $block = $this->getLayout()->createBlock(‘adminhtml/sales_order’);
    $this->getResponse()->setBody($block->toHtml());

b. Use block to: Blocks are used to create the interface for the system. Using the block is ordinarily to render it to html. This rendering is carried out when:

  • The system renders layout to html (when action calls renderLayout())
  • The parent block calls render function (maybe in block or in template)
<?php echo $this->getChildHtml(‘sales_order.grid.container’) ?>

c. Destroy a block: After a block render to html, the system doesn’t use it. The blocks generared before will be destroyed when the system ends request.

2. Events fired in block

a. Prepare Layout Event

core_block_abstract_prepare_layout_before: is fired before calling _prepareLayout function of the block.

core_block_abstract_prepare_layout_after: is fired after calling _prepareLayout function of the block. The parameter of this event is also block.

public function setLayout(Mage_Core_Model_Layout $layout){
    $this->_layout = $layout;
    Mage::dispatchEvent('core_block_abstract_prepare_layout_before', array('block' => $this));
    $this->_prepareLayout();
    Mage::dispatchEvent('core_block_abstract_prepare_layout_after', array('block' => $this));
return $this;
}

b. Render toHtml Event

core_block_abstract_to_html_before: is fired before the block is rendered to html. Block is the parameter of this event.

core_block_abstract_to_html_after: The Parameters of this event are block and transport. This event is fired right after the block is rendered to html.

final public function toHtml(){
    Mage::dispatchEvent('core_block_abstract_to_html_before', array('block' => $this));
    ...
    Mage::dispatchEvent('core_block_abstract_to_html_after',
array('block' => $this, 'transport' => self::$_transportObject));
    $html = self::$_transportObject->getHtml();
return $html;
}

The remaining parts of Block Chapter will  be continued for the next Magento Tutorial which must dig more deeply into and expand upon those simple definitions. Keep following for the full concepts of what you want 😉

Author

Why Magestore? We believe in building a meaningful & long-term relationship with you.

2 Comments

  1. Magento Developers Reply

    This is the most updated blog on magento. thanks for sharing such a cool stuff

    • Thanks for your comment. Hope our next posts will help you more 🙂

Write A Comment