cancel
Showing results for 
Search instead for 
Did you mean: 

Model Observer to modify HEAD - controller_action_layout_generate_blocks_after - breaks checkout?!

Model Observer to modify HEAD - controller_action_layout_generate_blocks_after - breaks checkout?!

 Hey folks, I'm attempting to insert some <link rel="dns-prefetch"> elements into the HEAD of all pages served by our Magento site. Unfortunately, the code below doesn't work - and weirdly enough, it only seems to break the final stage of checkout. We're running a pretty AJAX-heavy skin, I believe this one: http://themeforest.net/item/sahara-responsive-magento-themes/7844329?s_phrase=&s_rank=370

 

Anyway, after hacking and slashing through various documentation I believe this is the least-incorrect way to add what I want to the <HEAD> section of all pages. As mentioned, it seems to break checkout at the final order step, but not much else.

 

I've replaced the actual name of the customer with 'Customer' throughout these snippets:

 

app/code/local/Customer/DnsPrefetch/Model/Observer.php:

 

<? php
class Customer_DnsPrefetch_Model_Observer {
    public function alternateLinks() {
        $prefetch_urls = array(
            "//www.google-analytics.com",
            "//cdn.optimizely.com",
            "//connect.facebook.net",
            "//www.facebook.com");

        $headBlock = Mage::app()->getLayout()->getBlock('head');
        foreach($prefetch_urls as $url) {
            $headBlock->addLinkRel('dns-prefetch', $url);
        }
        return $this;
    }
}

 

app/code/local/Customer/DnsPrefetch/etc/config.xml:

 

<?xml version="1.0"?>
<config>
    <modules>
        <Customer_DnsPrefetch>
            <version>1.0.0</version>
        </Customer_DnsPrefetch>
    </modules>
    <frontend>
        <events>
            <controller_action_layout_generate_blocks_after>
                <observers>
                    <customer_dnsprefetch>
                        <type>singleton</type>
                        <class>Customer_DnsPrefetch_Model_Observer</class>
                        <method>alternateLinks</method>
                    </customer_dnsprefetch>
                </observers>
            </controller_action_layout_generate_blocks_after>
        </events>
    </frontend>
</config>

app/etc/modules/Customer_DnsPrefetch.xml:

 

<?xml version="1.0"?>
<config>
    <modules>
        <Customer_DnsPrefetch>
            <active>true</active>
            <codePool>local</codePool>
        </Customer_DnsPrefetch>
    </modules>
</config>

 

Tags (1)
2 REPLIES

Re: Model Observer to modify HEAD - controller_action_layout_generate_blocks_after - breaks checkout

Have you tried adding a check to see if $headBlock isn't null (then add your link rels)? It really sounds as if it can't find that block on the final checkout step and ends up causing an error.

Re: Model Observer to modify HEAD - controller_action_layout_generate_blocks_after - breaks checkout

@zenenjaimes: thanks, I'll try that (I had no idea it was A Thing). Do you have any idea if it's likely to occur on other pages, and/or why it only occurs at the end of checkout?