How To Insert Images Into Links In Drupal

ISSUE
I only struggled with this for about an hour. I’m not sure why Drupal hasn’t thought of this before, but it would be nice to have images as links once in a while. I needed one for my menu… a home link that was an image of a little house (how cute).

FIX
I read on a blog to edit the theme_menu_item_link function located in includes/menu.inc . I then added my <img tag to the Description. Well, that didn’t work. I investigated it further and looked at the code and instead put it in the Menu Link Title. That didn’t work either. I even tried the PHP function html_entity_decode, but that didn’t work either. It was still escaping the characters.

Anyway, after googling and reading up on how to do it, I tried to edit the template.php file with no luck. I read some other blogs and tried to edit some other files but it still kept escaping the code. I didn’t want to mess with the other files Drupal had put there.

I finally formed my own solution. I decided to edit the files Dupal put in. I looked for the l() function which is the function that actually forms links.

I added the following to the function l() in the file includes/common.inc on line 1598, right after the default options are declared in the $options array:

if (strpos($text, ‘<img’) === 0) {
$options[‘html’] = TRUE;
}

Yes, I know it’s unsafe but that’s the only way I could get it to NOT escape. I can now add HTML img script to the Menu Link Title and it display an image.

SPECIAL COMMENT
I’ll make this brief. I don’t know why Drupal didn’t think about images in link lists before. You think that would be obvious. The more and more I learn about Drupal, the more and more I realize how LESS flexible it is than I thought. I will say at least they have a community that can help, which is something that Microsoft seems to lack.

Come on, Drupal, think common sense.


Deprecated: Function get_currentuserinfo is deprecated since version 4.5.0! Use wp_get_current_user() instead. in /home/niles38/longlivethemonkey.com/syntaxnotes/wp-includes/functions.php on line 6031

Leave a Reply

Your email address will not be published. Required fields are marked *