Creating Dynamic Links in ‘My Account’ or in the ‘Toplinks’ Menu in Magento

Creating Dynamic Links in ‘My Account’ or in the ‘Toplinks’ Menu in Magento

I am sure many of you have once faced the necessity of creating links in ‘My account’ or ‘Toplinks’ menu. It is quite simple to implement – just add the following code into xml:

After that all you need is to create an appropriate controller (mymodule/myblock/index), a template for the content (mymodule/myblock.phtml), a block (mymodule/myblock) and to define them in the xml:

The only drawback is that the link is a static one, so it does not depend on any conditions or events. The easiest way to make it dynamic (dependent on events and conditions) is to use Magento events together with handles.

First off, let’s change the default handle <customer_account> to the custom handle <random_name_handle> in the aforementioned xml:

Next we assign an observer to the event <controller_action_layout_load_before>:

Creating an appropriate observer and a method for it:

The key point – $observer->getEvent()->getLayout()->getUpdate()->addHandle(‘random_name_handle’) – adds the handle <random_name_handle> and thereby adds the link if the condition is true.



1 comment

Post a new comment

top