I am trying to use a custom event observer to observe the controller_action_postdispatch
event and modify the response body directly by replacing opening head tag with + my custom block content. It is important that I make sure that it is an actual HTML page response, and not JSON or partial HTML in the case of an AJAX request. I would like to use the controller_action_layout_render_before
event to determine that the head block is present, set a flag, which the previously mentioned observer method will check before modifying the response.
The use case is to safely prepend my custom block inside the head tag of every page, taking priority order over other head content such as meta tags, and script tags.
This is where I am at right now (not far, got a little lost)...
<?xml version="1.0"?>
<config>
...
<global>
...
<events>
<controller_action_postdispatch>
<observers>
<intercept_response>
<class>module/observer</class>
<method>interceptResponse</method>
</intercept_response>
</observers>
</controller_action_postdispatch>
<controller_action_layout_render_before>
<observers>
<check_response>
<class>module/observer</class>
<method>checkResponse</method>
</check_response>
</observers>
</controller_action_layout_render_before>
</events>
</global>
...
</config>
class My_Module_Model_Observer {
protected $_isHead = FALSE;
public function checkResponse() {
if ( Mage::app()->getLayout()->getBlock('head')) {
$_isHead = true;
}
}
public function interceptResponse(Varien_Event_Observer $observer) {
$block = $observer->getBlock();
}
}
I would probably approach the solution with a more standard approach than intercepting output. You can do what you want by appending a child block in the layout and outputting it with a custom template file in your theme.
$theme/layout/local.xml
<default>
<reference name="head">
<!-- If you need a custom block, just swap in the type -->
<block type="core/template" name="prepend-head" template="page/html/prepend-head.phtml"/>
</reference>
</default>
$theme/template/page/html/head.phtml
Somewhere near the top of the file just add <?php echo $this->getChildHtml('prepend-head') ?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.