Community & Support

Controlling what link items goes in the $links variable on node.tpl.php

Hello Folks,

Somewhat new to drupal, and am using the standard blog content type. When I post an entry, after each node (depending on the user permissions) there are a list of links such as : users blog, more, Add a new comment etc. I would like to have the Add a new comment line, but I do not want the users blog link on there. Where do I configure this list of links? The commented documentation says that it is output from the themed_links() function, but I don't see where particular links can be configured here.

Thanks for any help.

Comments

Theme Override

You use hook_links_alter in your theme's template.php file to manage this.

function YOURTHEMENAMEGOESHERE_links_alter(&$links, $node) {
  foreach ($links as $module => $link) {
    if (strstr($module, 'blog')) {
      // Remove blog_usernames_blog
      unset($links[$module]['blog_usernames_blog']);
    }
  }
}

Essentially, this code loops over all of the $links and to find the matching one, in this case only for the "blog" module and the specific link "'blog_usernames_blog'" and then removes it (unset)

Be sure to clear your cache after adding this function, so your theme finds it.

---------------------------------
Steven Wright
Drupal Developer in Washington, DC
http://www.enterana.com

_

Not quite-- you're mixing theme overrides and hooks. See #324796: A link called "$user's blog" is printed after each blog post and cannot be removed for how to do this.

_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.

Works for me

The code I included came right out of my own template.php file where its been working for me just fine.

Is there something inappropriate about making the change there? I'm good enough developer to know that just because it seems to work doesn't mean I should necessarily do it that way = P

---------------------------------
Steven Wright
Drupal Developer in Washington, DC
http://www.enterana.com

_

Don't know what you tell you, but I can assure you the posted code will not work-- there is no such thing as hook_links_alter. There's http://api.drupal.org/api/function/theme_links, http://api.drupal.org/api/function/hook_link, and http://api.drupal.org/api/function/hook_link_alter.

Also afaik, hooks will not be recognized in template.php (it's processed too late in the cycle)-- they must be implemented in a module.

So that means this change can be made one of two ways.

In a module with a hook:

function mymodule_link_alter(&$links, $node) {
  if (isset($links['blog_usernames_blog'])) {
    unset($links['blog_usernames_blog']);
  }
}

or in template.php with a theme override:

function mytheme_links($links, $attributes = array()) {
  if (isset($links['blog_usernames_blog'])) {
    unset($links['blog_usernames_blog']);
  }
  return theme_links($links, $attributes);
}

_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.

Absolutely Correct

My apologies - I copied the wrong code and WorldFallz is correct.

I remember working through this and trying different solutions. My template file includes:

function phptemplate_links($links, $attributes = array()) {
    unset($links['blog_usernames_blog']);
    return theme_links($links, $attributes);
}

which is what WorldFallz indicated is the correct solution.

The "links_alter" code was left in my file incorrectly, and when I went looking for the code to share I grabbed that without thinking about it.

Thank you for the correction!!!

---------------------------------
Steven Wright
Drupal Developer in Washington, DC
http://www.enterana.com

Hello I was browsing through

Hello I was browsing through and wondered if this code could also be used to INSERT something into the links field for that theme's module. For instance, a ratings widget.

Thanks

"in" is still coming

Hi In my site in the blog posts - "In username's blog" was coming on the top of the blog post and "username's blog" was coming at the bottom. I modified my template file as suggested by you and "username's blog" got removed, but "in" is still coming at the top of the post. Any idea how to remove this "in"?

Saday

thanks WorldFallz for your

thanks WorldFallz for your post. you saved me :)

arpita

mytheme_links in D7

Can somebody update the example above to do the same in D7 with a theme override?

Already using mytheme_links()

Hi:

I am a bit confused, I am already using the mytheme_links() in my template.php. I got the code from another post to add extra and be able to create menus with rounded corners using background images.
My question is:

Is mytheme_links() the same function used for all links in the website or for the $links in a node?

Many thanks

_

_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.