Enable javascript for live chat

Knowledge Base Overview

Attribute rel with nofollow

Share on Tumblr

Let me first say that this is not about bickering or complaining. This is simply about helping and providing a solution for a problem that has already been addressed but unfortunately is not implemented yet.

I guess the problem with the open issue is someting we have to accept and deal with in free OSS and free participation or contribution to a project.

Current issue status

The problem is about the support for the rel="nofollow" attribute in an anchor element. Although you can define the attribute in Zend Framework it is not actually printed in the navigation view helper menu. This is still an open issue in the Zend Framework as of 12/30/2011. I found two trackers for this 

First we have an issue ZF-7733 asking for support of the rel attribute in Zend_Navigation_Page. While I quite understand why it was denied I wonder why there is no progress in ZF-9300.

Although, I agree why the suggested patch has not been implemented because there are more options than just the "nofollow". If you can't wait for the official support and need rel="nofollow" in your menu links here's what you have to do.

Adding rel attribute

The frustrating part with this open problem is that the following is already available and makes you wonder why does it not work?

// if you already have your navigation page object
$navPage->setRel(array('nofollow'=>'nofollow'));
// or the config you would add to a navigation page object
array(
  'uri'=>'/your-link',
  'label'=>'My Link',
  'rel'=>array('nofollow'=>'nofollow')
);

Note in both cases that rel expects an array and that the index is a string with the same value, i.e. you cannot pass an array with numeric index.

So far so good. The problem at this point is that it only works for meta tags but up to version 1.11.11 the navigation helper doesn't do anything with it for links (the <a> anchor element). Here come the actual fixes.

Updating htmlify

Note: Before you begin a little warning. You should usually not make changes in the main library. If you do and later upgrade your Zend Framework make sure to verify the current status of the fixes. You've been warned!

Following are the two fixes required to have a rel attribute with the value in your anchor element. You have to update a method named htmlify and unfortunately in two places.

First we have an abstract class named Zend_View_Helper_Navigation_HelperAbstract and depending on your version the htmlify method should be around line 675. The second class is Zend_View_Helper_Navigation_Menu (line ~209) actually overridding htmlify in the abstract helper class.

Now you add the following in both methods; I did it just before the return. I basically replicated what you see in the $href condition and the menu class.

// does page has rel
if ( $rel = $page->getRel() ) {
    $attribs['rel'] = $rel;
}

That should do it and add the rel="nofollow" string to the anchor element. In fact it should add whatever you add to rel in the navigation page. What could be added is a check to see if the value is allowed according to the HTML5 Linktypes specification.

blog comments powered by Disqus