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
Mage_Page_Block_Html_Head
. They don't override theaddLinkRel
method I'm trying to override though. Is there a way I can still override that method?