Want to get Lullabot article, videocast, and podcast announcements delivered right to your in-box?
Let us know your email address (we won't share it) and we'll let you know when anything exciting happens.
How to use links in 4.8
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
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!
Thanks! Ber started this
Thanks! Ber started this patch and then I took it to a whole new level :-)
Wow. When will 4.8 be
Wow. When will 4.8 be ready?
... Just kidding.
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 ;-)
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?
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.
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.
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 :)
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
Removing the # so it’s
Removing the # so it's 'title' did it for me.
HTH.
--D
Thanks for the patch Ted!
Thanks for the patch Ted! It's really useful for me.
Post new comment