3

I am trying to override Magento's addLinkRel method to set custom canonical tags: I want my shop's canonical tags to point to the main shop's pages.

So I created code/local/Fkoessler/MyProduct/etc/config.xml:

<config>
    <modules>
        <Fkoessler_MyProduct>
            <version>0.1</version>
        </Fkoessler_MyProduct>
    </modules>
    <global>
        <blocks>
            <page>
                <rewrite>
                    <html_head>Fkoessler_MyProduct_Block_Html_Head</html_head>
                </rewrite>
            </page>
        </blocks>
    </global>
</config>

And code/local/Fkoessler/MyProduct/Block/Html/Head.php:

class Fkoessler_MyProduct_Block_Html_Head extends Mage_Page_Block_Html_Head
{
    /**
     * Add Link element to HEAD entity
     *
     * @param string $rel forward link types
     * @param string $href URI for linked resource
     * @return Mage_Page_Block_Html_Head
     */
    public function addLinkRel($rel, $href)
    {
        $href = str_replace('/paris', '', $href);
        $href = str_replace('/nimes', '', $href);
        $href = str_replace('/niort', '', $href);
        $this->addItem('link_rel', $href, 'rel="' . $rel . '"');
        return $this;
    }
}

However, my custom method is not picked up at all. What am I doing wrong?


EDIT:

So I understand that using an observer is better than using class inheritance. Here is how I set it up:

etc/modules/Fkoessler/Canonical.xml
<?xml version="1.0"?>
<config>
    <modules>
        <Fkoessler_Canonical>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
              <Aitoc_Aitsys/>
            </depends>
        </Fkoessler_Canonical>
    </modules>
</config>

-

app/code/local/Fkoessler/Canonical/etc/config.xml
<?xml version="1.0"?>
<config>
    <modules>
        <Fkoessler_Canonical>
            <version>0.1</version>
        </Fkoessler_Canonical>
    </modules>
    <global>
        <models>
            <fkoessler_canonical>
                <class>Fkoessler_Canonical_Model</class>
            </fkoessler_canonical>
        </models>
        <events>
            <core_block_abstract_to_html_before>
                <observers>
                    <fkoessler_catalog_core_block_abtract_to_html_before>
                        <type>singleton</type>
                        <class>fkoessler_canonical/observer</class>
                        <method>coreBlockAbstractToHtmlBefore</method>
                    </fkoessler_catalog_core_block_abtract_to_html_before>
                </observers>
            </core_block_abstract_to_html_before>
        </events>
    </global>
</config>

-

app/code/local/Fkoessler/Canonical/Model/Observer.php
class Fkoessler_Canonical_Model_Observer
{

    public function coreBlockAbstractToHtmlBefore($observer)
    {
        error_log('im observing you');
        $block = $observer->getBlock();
        if ($block instanceof Mage_Page_Block_Html_Head) {
//            $block->removeItem('link_rel', [the current URL]);
//            $block->addLinkRel('canonical', [edited URL]);
        }
    }
}

So I managed to enter my coreBlockAbstractToHtmlBefore method when the core_block_abstract_to_html_before is fired. Now how can I get the canonical url set by Magento core to modify and return it?

$observer->getBlock(); returns an instance of Mage_Page_Block_Html

  • 1
    obvious reason maybe, is the head block already overwritten? – Sander Mangel Jun 26 '15 at 13:54
  • That's it!! The Fooman_Speedster community module is already overriding Mage_Page_Block_Html_Head. They don't override the addLinkRel method I'm trying to override though. Is there a way I can still override that method? – fkoessler Jun 26 '15 at 14:20
  • Check my answer :) – Sander Mangel Jun 26 '15 at 14:52
6

You can use an observer to reset they value.

config.xml - add an observer

<core_block_abstract_to_html_before>
    <observers>
        <[namespace]_[module]_core_block_abstract_to_html_before>
            <type>singleton</type>
            <class>[Namespace]_[Module]_Model_Observer</class>
            <method>coreBlockAbstractToHtmlBefore</method>
        </[namespace]_[module]_core_block_abstract_to_html_before>
    </observers>
</core_block_abstract_to_html_before>

observer.php

public function coreBlockAbstractToHtmlBefore($o)
{
    $block = $o->getBlock();
    if($block instanceof Mage_Page_Block_Html_Head) {
        $url = Mage::helper('core/url')->getCurrentUrl();
        if (Mage::registry('current_product')) {
           $url = Mage::registry('current_product')->getProductUrl();
        } else if (Mage::registry('current_category')) {
           $url = Mage::registry('current_category')->getUrl();
        }
        $block->removeItem('link_rel', $url);
        // do your magic with the $url var here
        $block->addLinkRel('canonical', $url);
    }
}
  • My observer class isn't being picked up by Magento. Any idea why..? – fkoessler Jun 26 '15 at 15:27
  • Oh make it an instance of the speedster block – Sander Mangel Jun 26 '15 at 16:10
  • So I managed to enter my observer method. (not sure if the depends Aitoc_Aitsys is needed). Now how can i retrieve the canonical url to modify and return it? :) – fkoessler Jun 27 '15 at 9:16
  • Small update on my answer, didn't check it so might need some tweaking – Sander Mangel Jun 27 '15 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.