3

I've been through no end of examples and "nearly what I want" questions and answers, but can't find what I want, I hope the community can help me out here.

What I am trying to acheive is to conditionally add an external link to the customer_account_navigation block, that leads to a 3rd party service in a new tab/window.

I have a brand new module to do this in, for example's sake this namespace/module will be called Cygnus_ExternalCustomerLink.

The main thing to consider is that we're using a highly customised theme and there are 3rd party modules that are also adding items to this navigation. I can handle these (they are not rewiting the block, but adding items via layout.xml) but I wanted to declare them just in case.

So at first I looked to using addLink to achieve this, howeve I am limited in that addLink won't take an external link (as far as I know!) and also while I can get the addLink layout.xml to work it won't actually display the link! (I debugged the addLink function and can see it being called for my link, but by the time navigation.phtml is parsed it is gone again!

So I'm looking for some guidence on how to add an external link here, without overriding the template completely (in case other modules wish to get involved). Otherwise an internal link to get me started would be great!

Here's my code so far. I have tried an alternative method that involves adding a child block that adds a link to the parent, found here (Create link dynamically in customer account navigation) but I can't get this to work either.

config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Cygnus_ExternalCustomerLink>
            <version>0.3.9</version>
        </Cygnus_ExternalCustomerLink>
    </modules>
    <frontend>
        <layout>
            <updates>
                <cygnus_externalcustomerlink>
                    <file>cygnus_externalcustomerlink.xml</file>
                </cygnus_externalcustomerlink>
            </updates>
        </layout>
    </frontend>
    <global>
        <helpers>
            <Cygnus_ExternalCustomerLink>
                <class>Cygnus_ExternalCustomerLink_Helper</class>
            </Cygnus_ExternalCustomerLink>
        </helpers>
    </global>
</config>

cygnus_externalcustomerlink.xml (Layout)

I try 2 methods here, first is a bog standard addLink, the second is my attempt at the solution linked above

<?xml version="1.0"?>
<layout version="0.1.0">
    <customer_account>
        <reference name="customer_account_navigation">
            <!-- this is my first attempt to add any old link to the nav -->
            <action method="addLink">
                <name>Test Name</name>
                <path>customer_account/index</path>
                <label>Test Label</label>
            </action>
            <!-- this is my attempt to add a block that effects the parent --> 
            <block 
                type="Cygnus_ExternalCustomerLink/customer_link" 
                as="cygnus_externalcustomerlink_customer_link" 
                name="cygnus_externalcustomerlink_customer_link"
            >
                <action method="addCustLinkToParentBlock" />
            </block>
        </reference>
    </customer_account>
</layout>

Block/Customer/Link.php (the add to parent from new block method)

This block doesn't fire at all, if you see the dodgy print <pre>, this is never hit.

<?php
class Cygnus_ExampleCustomerLink_Block_Customer_Link extends Mage_Core_Block_Abstract
{
    public function addCustLinkToParentBlock()
    {
        print "<pre>Testing!</pre>";
        $parent = $this->getParentBlock();
        if ($parent) {
            if (true) {
                $parent->addLink(
                    'MyLabel2',
                    'customer_account/index',
                    'MyTitle2'
                );
            }
        }
    }
}

PS> As stated I have tried a number of solutions in posts from here, and elsewhere but all to no avail. I may be getting solution blindness here but I'm happy to try a method again.

EDIT : While ths issue is resolved below, this is not an accurate method above on how to add an external link.

I will elaborate, in customer_account there is only an addLink function available which only takes an internal URL. so I added my own "addLink" alternative function. I did this by overloading Mage_Customer_Block_Account_Navigation in my module, and adding the below code:

class Cygnus_ExternalCustomerLink_Block_Customer_Account_Navigation extends Mage_Customer_Block_Account_Navigation
{

    public function addExternalLink($title)
    {
        //we have to hack the url value here to insert the external param, saved extensive template overriding
        //note the missing tailing " quote, we are using a string with hacked in attributes as the "url" of the 
        //link, this is very messy however as the customer account menu has much less functionality than the top
        //menu this was the path of least resistance...

        $url = 'http://my.external.url' . '" target="_blank" rel="nofollow';

        $this->_links[$title] = new Varien_Object(array(
            'label'         => $title,
            'url'           => $url,
            'title'         => $title
        ));

        return $this;

    }

}

Then obviously update the method attribute wherever you update this block in the layout (in my case it was local.xml).

1 Answer 1

0

I think you are simply missing the block definition from your module's config.xml.

<?xml version="1.0"?>
<config>
    <modules>
        <Cygnus_ExternalCustomerLink>
            <version>0.3.9</version>
        </Cygnus_ExternalCustomerLink>
    </modules>
    <frontend>
        <layout>
            <updates>
                <cygnus_externalcustomerlink>
                    <file>cygnus_externalcustomerlink.xml</file>
                </cygnus_externalcustomerlink>
            </updates>
        </layout>
    </frontend>
    <global>
        <helpers>
            <cygnus_externalcustomerlink>
                <class>Cygnus_ExternalCustomerLink_Helper</class>
            </cygnus_externalcustomerlink>
        </helpers>
        <blocks>
            <cygnus_externalcustomerlink>
                <class>Cygnus_ExternalCustomerLink_Block</class>
            </cygnus_externalcustomerlink>
        </blocks>
    </global>
</config>

I would also suggest switching from upper case to lower case with the block and helper definitions simply to match with Magento standards.

3
  • Thanks David, I agree I was missing this but I did have the <blocks> parameter in an earlier iteration of my testing, good spot. However the issue turned out to be that my module was fine, the whole menu was being overridden in local.xml. I completely over-saw this, face-palm. Apr 29, 2015 at 8:23
  • @cygnusdigital feel free to either add your own answer or close the question if you do not feel it would help future site users. Glad you sorted it out though Apr 29, 2015 at 8:24
  • No no, it's helpful, correct and the resolution (which deserves less attention, so embarrised about my theme oversight!) is in my comments. I feel your answer is still valid so we'll leave it ticked :) Apr 29, 2015 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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