How to insert an image into primary links in Drupal

Drupal normally provides powerful and secure menu system to all of us. We are able to manipulate primary links and secondary links seamlessly and easily with little effort. However, sometimes we would like to insert an image instead of text. I found a good snippet but it does not work for me. This snippet is just for menu item but actually, I am talking about primary links or secondary links. Starting in Drupal 5, primary links and secondary links are not formatted by the theme engine, instead, a list of arrays are passed to the theme directly. By default, all menus are passed to check_plain() to prevent unexpected security holes so we cannot insert HTML tag here include . Fortunately, this is Drupal. We can customize theme to let us insert just only img tag.

Since the theme engine populated primary_links and secondary_links by assuming plain text, we have to enable html option so that l() will not call check_plain(). Below is my patch to the excellent garland theme.

diff -u template.php.orig template.php
--- template.php.orig     2006-12-13 04:32:18.000000000 +0700
+++ template.php      2007-05-04 10:41:29.000000000 +0700
@@ -61,6 +61,15 @@
       $output .= "
    \n". $secondary ."
\n"; $vars['tabs2'] = $output; } + $old_links = $vars['primary_links']; + $new_links = array(); + foreach ($old_links as $link) { + if (strpos($link['title'], ') === 0) { + $link['html'] = TRUE; + } + $new_links[] = $link; + } + $vars['primary_links'] = $new_links;   // Hook into color.module if (module_exists('color')) {

At this point, we are able to specify in path of a primary link.

Tags: , , ,

Post new comment