Home

Lullabot

Lullabot Ideas

We know stuff. We empower you to know stuff too.

On Site Drupal Training

We'll come to you! Graduate from your own on-site courses and become Drupalistas!

How to use links in 4.8

Article by Ted SerbinskiMay 18, 2006 - 4:57pm

Just today a patch that I developed was committed to the Drupal 4.8 (or is that 5.0?) branch that restructured the use of links in core, particulary links from hook_link() and menu_primary_links() and menu_secondary_links(). The upgrade docs I wrote talk about this change on a technical level.

But what about on a practical level? Here are some examples of what you can do now with ease :-)

  • I want to add icons to the "read more", "add new comment", etc. links. How can I?

    Simple. With the changes in core now, each link generated from hook_link() has an associated class with it. In this case, the CSS classes are "node-read-more" and "comment-add". In your CSS file you can now set a background image for these links and poof, instance icon! No more fuss! Just search the source to find the other class names for other links.

  • I want each menu item in my primary links to have a specific class so I can do XYZ cool CSS formating. How can I do this without regexes?

    See above! Because menu_primary_links() and menu_secondary_links() return structured data, if you pass these through theme('links') as recommended, each of the links will come out with a class associated with it. Each of these items has a class name in the form X-Y-Z, which should be a unique class per link. This allows fancy CSS formatting, such as using CSS for images as menu items.

  • I don't like the link title "add new comment", it's too direct and impersonal. I want it to say "please add what you think". How can I do this?

    Since menu links are structured now, this is very simple, however, this is a small caveat. In phpTemplate, the $links variable available in node.tpl.php has already been sent to theme('links'), as a result you have your links, but they aren't structured anymore!

    In this case, what you need to do, is something like this in node.tpl.php:

    <?php
    //get the structured links
    $links = $node->links;
    $links['comment_add']['#title'] = t('please add what you think');

    print
    theme('links', $links);
    ?>

    As simple as that. You can also easily change any of the hrefs and attributes as well.

  • I don't want comment module to add *any* links. How can I prevent this?

    Well one way would be to edit node.tpl.php and filter out all links that have the key "comment" in them. But we don't want to add this extra logic to our template file, our designers don't like code :-) So instead, we can use the new hook_link_alter() and do this at the module level.

    Create a new module and define the new hook_link_alter() function:

    <?php
    function mymodule_link_alter(&$node, &$links) {
      foreach (
    $links AS $module => $link) {
        if (
    strstr($module, 'comment')) {
          unset(
    $links[$module]);
        }
      }
    }
    ?>

    And voila, no more links from comment module.

Wow, isn't theming so much easier now? I thought so to!

Comments

Brad Landis (not verified) on May 18, 2006 - 5:44pm

Awesome

There's been quite a bit of fuss over this in the forums, and this looks like it'll fix a lot of those problems. Awesome job to whoever came up with a patch!

May 18, 2006 - 6:13pm Ted Serbinski

Thanks! Ber started this

Thanks! Ber started this patch and then I took it to a whole new level :-)

Erik Mallinson (not verified) on May 18, 2006 - 6:03pm

Wow. When will 4.8 be

Wow. When will 4.8 be ready?

... Just kidding.

May 18, 2006 - 6:10pm Ted Serbinski

The freeze is 1 Sept. Add

The freeze is 1 Sept. Add some time to finalize and cleanup and that would be the Nov range. Provided we get back on a schedule this release, but it all points to yes. But don't quote me and use Drupal 4.8 at your own risk ;-)

Ian (not verified) on June 6, 2006 - 9:33am

this looks very useful

Hey Ted, this looks really useful. Nice work. Does it rely on anything else so far done in the development version of 4.8/HEAD?

June 6, 2006 - 10:55am Ted Serbinski

Nope this patch was

Nope this patch was standalone :-)

There are some subsequent patches I'm working on though that are related to this, but nothing with any major depedencies though.

Anonymous (not verified) on August 9, 2006 - 2:51am

Other modules

It seems that any added modules which don't use this newer system breaks things. What would be more than wonderful is if there were a 4.7 patch that included some checking for those so the whole site isn't taken down.

In a more general sense it'd be nice if Drupal would just reject outdated and incompatible modules instead of dying, but that would be a lot harder to do.

Rpg games (not verified) on November 7, 2007 - 10:58pm

New modules

Yeah, we have to be careful with what modules we add. I think it brings a little more fun in our every day life haha :)

Anonymous (not verified) on October 10, 2006 - 4:20pm

I remember this post from a

I remember this post from a while back and finally got around to cracking open the new CVS version of drupal tonight...

I just wanted to try and see theming of links for myself but can get the dame thing to function

returning $links['comment_add']['#title'] on its own seems to work but cant pass the array through the theming function

tks
M

DaveNotik (not verified) on December 3, 2006 - 11:54pm

Removing the # so it’s

Removing the # so it's 'title' did it for me.

HTH.

--D

Peter (not verified) on November 15, 2006 - 11:09am

Thanks for the patch Ted!

Thanks for the patch Ted! It's really useful for me.

mpg (not verified) on July 23, 2008 - 8:28pm

this did not work for me, help

I tried this code below int the node.tpl.php file and it did not work in 5.5...

Please help!

<?php
//get the structured links
$links = $node->links;
$links['comment_add']['#title'] = t('please add what you think');

print
theme('links', $links);
?>

Xrumer (not verified) on October 3, 2008 - 5:49am

Thanks for the patch Ted!

Thanks for the patch Ted! It's really useful for me.

About this 'bot

Ted Serbinski

Ted graduated from Cornell University with a degree in Information Science, Systems, and Technology and a minor in Computer & Electrical Engineering. While working on a 5 month coop at Intel in Massachusetts, Ted quickly realized that building web based tools was far more his forte than debugging...

more

Recent

Drupal Voices 160: Moshe Weitzman on Page Rendering in Drupal 7

Podcast 9.02.2010

Drupal Voices 159: John Albin Wilkins on Drupal 7 Theming

Podcast 9.01.2010

Drupal Voices 158: Emma Jane Hogbin on PHP for Designers

Podcast 8.31.2010

Command Line Basics: More Editing with Vi/Vim

Video 8.31.2010

Lullabot's Back to School Sale

Blog 8.30.2010

Popular

Drupal Voices 160: Moshe Weitzman on Page Rendering in Drupal 7

Podcast 9.02.2010

Drupal Voices 159: John Albin Wilkins on Drupal 7 Theming

Podcast 9.01.2010

Photo galleries with Views Attach

Article 6.01.2009

Announcing BeautyTips, a jQuery Tooltip Plugin

Article 10.20.2008

Install a Local Web Server on Ubuntu

Video 11.14.2007
 
  • Home
  • Services
  • Events
  • Ideas
  • Store

Connect the Bots:

Twitter Facebook YouTube blip.tv All Posts Newsletter
  • Ideas
  • Blog
  • Podcasts
  • Videos
  • About
  • Contact
  • Jobs
  • Services
    • Training
  • Events
    • Training Workshops
    • Other Events
    • Conferences
    • Calendar
  • Products
    • Videos
    • Books
    • Swag
  • Ideas
    • Blog
    • Podcast
    • Videos
  • About
    • Philosophy
    • Team
    • Presskit
  • Contact
    • General
    • Work Inquiries
    • Mailing List