Search code examples
templatesmagentolayoutblock

Dynamically add links to 'my account navigation' in magento


I'm creating a Magento Extension and want to be programatically add links to the 'My account' navigation. I've read the following thread (Magento - How to add/remove links on my account navigation?) and the sites it references, but they only talk about adding a link statically.

By adding the following to a layout file in my module I can get a new link to appear in the customer account navigation.

<customer_account>
    <reference name="customer_account_navigation">
        <action method="addLink" translate="label" module="mymodule">
            <name>modulename</name>
            <path>mymodule/</path>
            <label>New link</label>
        </action>
    </reference>
</customer_account>

How can I make it so that this links appearance depends on the results of a method call to one of my extensions models.


Solution

  • You have to use event-observer functionality of magento The event you have to use it "controller_action_layout_load_before" In your module's config.xml

    <controller_action_layout_load_before>
        <observers>
            <uniquename>
                <type>singleton</type>
                <class>Package_Modulename_Model_Observer</class>
                <method>customlink</method>
            </uniquename>
        </observers>
    </controller_action_layout_load_before>
    

    and in the corresponding observer.php use the following code

    public function customlink(Varien_Event_Observer $observer)
    {
        $update = $observer->getEvent()->getLayout()->getUpdate();
        $update->addHandle('customer_new_handle');
    }
    

    and in local.xml write

    <customer_new_handle>
        <reference name="customer_account_navigation">
            <action method="addLink" translate="label" module="mymodule">
                <name>modulename</name>
                <path>mymodule/</path>
                <label>New link</label>
            </action>
        </reference>
    </customer_new_handle>