I'm coming from a Wordpress background, where many of the templates have (pointless) icons next to much of the post metadata. E.g. a pencil for "written by" or a small clock next to the time/date, etc.

Though I don't want to clutter things up that much, I do want to add a small speech bubble next to "Add comment" and a Printer icon next to "printer-friendly version", etc. How would I go about this? I'm not even sure how to break down $links, let alone discovering what I would replace that with!

Thanks for any help you can give.

Comments

Anonymous’s picture

Give CSS a try..
Check if the links have classnames or ids, if not, check where they are.

Then attempt to use css and a background-image with some margin or paddings to play with positioning of the text (if the text is needed).

If you want to completely remove the text and swap it with an image, you can display the link as block, then text-indent: -10000px; to hide the text.

does this help?

Tip: use Firefox with the Web Developer Extension.

divrom’s picture

Thanks, Steve, but that's the first thing I tried.

I'd noticed this on 1 or 2 other sites, so I figured that's how they did it; but there are no classes in sight.

So, all I can think is that I need to break open the $links, or find somewhere else to add classes. Any ideas?

asanjuanc’s picture

it is posible on css

green monkey’s picture

thanks for this post "then text-indent: -10000px; to hide the text." was nice to learn

JohnG-1’s picture

.comment_delete a {
  display: block;
  text-indent: -10000px;
  background:url(icons/comment.png)  no-repeat;
  }

This works to hide the link text, but the display: block creates a linebreak before and after the icon.
Does anyone know how to do this without inheriting the linebreaks ?
... I want all my icons in a row ;-)

SteveK’s picture

try this if you wanted to replace it with a graphic and remove the text:

.classname {
  display: block;
  float: left;
  text-indent: -999px;
  overflow: hidden;
  background: url('image.gif') no-repeat;
}

---
I work here: Raincity Studios.com
I hang out here: Steve Krueger.com

Samat Jain’s picture

As far as I know, there is no easy way to do this.

But I am doing it for the 4.7 version of my Sands theme (I can never stop plugging it...). I'm doing it the niave (and probably slow) method of just rewriting $links in the corresponding theme function to have classes and IDs, i.e.:

function sands_links($links, $delimiter = ' • ') {
  if (!is_array($links)) {
    return '';
  }

  /* Maps a link to it's associated class */
  $link_to_id = array(
    t('edit') => 'edit',
    t('delete') => 'delete',
    t('reply') => 'reply',
    t('add new comment') => 'add-comment',
    t('Jump to the first comment') => 'comment',
    t('read more') => 'read-more',
    t('printer-friendly version') => 'print',
    t('add child page') => 'add-child-page',
    t('calendar') => 'calendar',
  );

  $new_links = array();

  foreach ($links as $link) {
    foreach ($link_to_id as $text => $id) {
      if (strpos($link, $text)) {
        $link = str_replace('<a ', "<a class=\"icon-$id\" ", $link);
      }
    }
    $new_links[] = $link;
  }

  return implode($delimiter, $new_links);
}

I've yet to do benchmarking on how slow this is... But it certainly looks good.

You can see the theme in development at my personal homepage. I've yet to commit this code to CVS but will do so when I get time.

__
Personal home page | Rhombic Networks: Drupal-friendly web hosting

divrom’s picture

That looks really good, Samat. Thanks.

So, what is the file that you're putting that code into?

Samat Jain’s picture

template.php, forgot to mention.
__
Personal home page | Rhombic Networks: Drupal-friendly web hosting

Anonymous’s picture

that does look good.

this is something we need and I can see myself needing this in the future as well.

marlowx’s picture

i am a very new drupal user but i'd love to try this, but where do i upload the icons?

i noticed that there are some icons in the misc directory, do i put them there?

thanks guys!

Samat Jain’s picture

For the code I have above, inside a directory called "icons" in the themes directory.

I've these icons incorporated into my themes Sands and Sands_CSS. You may want to look at them for examples.
__
Personal home page | Rhombic Networks: Drupal-friendly web hosting

t4him’s picture

Thank You Samat for posting this. You made my life easier. EXCELLENT JOB!
Your explination for not doing it 'other ways' was also beneficial. Mainly because that IS the way I was going to do it, but decided first to check the forums.

randomtimes’s picture

I've stuck this in my template.php and it doesn't appear to make any difference? Is there something that needs to be called somewhere else to get it to go?

Samat Jain’s picture

The function is called "sands_links," so it will only be called if your theme is called "sands." If your theme is named blah, the function needs to be named "blah_links."
__
Personal home page | Rhombic Networks: Drupal-friendly web hosting

randomtimes’s picture

I had changed the function name, but I didn't realise it was tied to the theme. Looks like it's working now, thank you :)

jferjan’s picture

Can u please tell me how to modify this code to display classes for delimiters and links. I would like to add something like span class=icon-$id to delimeters.

edit:
ok i just added
$link = str_replace('</a>', "</a><span class=\"del-$id\"> | </span>", $link);

so my code now looks like this:

function phptemplate_links($links, $delimiter = '  ') {
if (!is_array($links)) {
return '';
}

$link_to_id = array(
t('predlagaj za naslovnico') => 'predlagaj',
t('dodaj nov komentar') => 'dodaj',
t('preberi več') => 'preberi',
t('obveščaj me') => 'obvescaj',
t('umakni oznako') => 'umakni',
t('št. komentarjev: ') => 'stevilo',
t('št. novih komentarjev: ') => 'novih',
t('izbriši') => 'izbrisi',
t('uredi') => 'uredi',
t('odgovori') => 'odgovori',
t('citiraj') => 'citiraj',
);

$new_links = array();

foreach ($links as $link) {
foreach ($link_to_id as $text => $id) {
if (strpos($link, $text)) {
$link = str_replace('<a ', "<a class=\"icon-$id\" ", $link);
$link = str_replace('</a>', "</a><span class=\"del-$id\"> | </span>", $link); 
}
}
$new_links[] = $link;
}
return implode($delimiter, $new_links);
} 

i also saved template.php in utf-8 format to allow nonenglish characters.

www.ferjan.net

asanjuanc’s picture

I have a site and i do it completely in css

Daniel Garcia Peña

for example

.read-more{
    background-image: url(add.png);
    background-repeat: no-repeat;
    padding-left: 20px;
}
Samat Jain’s picture

You've only styled one link--many other links do not have classes or IDs (e.g. add comment, etc).
__
Personal home page | Rhombic Networks: Drupal-friendly web hosting

noizyboy’s picture

I'm pretty new to both drupal and php, so I'm not sure if this is the most elegant hack in the world, but it seems to be working for me.

I changed the common.inc 'l' function to check the $text field, and send an image back in the return if it found certain keywords.

so, somewhere around line 1511 of common.inc, instead of just...

return '<a href="'. check_url(url($path, $query, $fragment, $absolute)) .'"'. drupal_attributes($attributes) .'>'. ($html ? $text : check_plain($text)) .'</a>';  	

...I've now got...

  if (strpos($text, "read more") !== FALSE) {
  	return '<a href="'. check_url(url($path, $query, $fragment, $absolute)) .'"'. drupal_attributes($attributes) .'><img src="/images/readmore.gif" valign="absmiddle" hspace="4" border="0">'. ($html ? $text : check_plain($text)) .'</a>';
  } elseif (strpos($text, "comment") !== FALSE) {
  	return '<a href="'. check_url(url($path, $query, $fragment, $absolute)) .'"'. drupal_attributes($attributes) .'><img src="/images/comments.gif" valign="absmiddle" hspace="4" border="0">'. ($html ? $text : check_plain($text)) .'</a>';
  } else { 
  	return '<a href="'. check_url(url($path, $query, $fragment, $absolute)) .'"'. drupal_attributes($attributes) .'>'. ($html ? $text : check_plain($text)) .'</a>';  	
  }

to remove the text part of the links and just leave the icons, just drop the . ($html ? $text : check_plain($text)) . bit from the relevant lines.

obviously, you run the risk of a non-comment/readmore link that has those keywords in the $text variable, and thus icons turning up where you don't necessarily want them, but I think with a bit of fine-tuning it'd probably do the job required reasonably well.

I think, in fact (I've just been playing with this code this afternoon, so it's even evolved as I've written this post), that you'd be better off using the title text that gets sent, ie the "Jump to the first comment of this posting", "Add a new comment to this page" and similar that get passed to the l function as part of the $attributes array. Hmmm.

Anyway, I've got this code running on my site (still under heavy development) - wellingtonista.com

bradlis7’s picture

I think the l function is a called quite a bit, so I'd avoid making the code there any more loaded than necessary.

--
Bradlis7.com | Churchofchristnet

noizyboy’s picture

indeed it is, but I wouldn't have thought adding an if..elseif...elseif..else in there would have added too much in the way of processing overhead.

anyone know of a way of testing page processing speeds so i can test this with and without the hacked code?

JohnG-1’s picture

developer module tracks processing times etc : http://drupal.org/project/devel

Samat Jain’s picture

Generally, modifying core Drupal files is not encouraged, because it makes upgrading a pain. Granted, the "l" function will probably not be changed anytime during the 4.7 release cycle...
__
Personal home page | Rhombic Networks: Drupal-friendly web hosting

chrishaslam’s picture

I've just used this to style $links in a more granular fashion, thanks for the tip and good work with sands_css!

---------------------------
Managed Drupal hosting from Ixis IT

pushkar’s picture

Ok, I know its easier to do this in Drupal 5. But for newbies like me, here are some instructions to help you get started.

Background

In your code drupal theme.inc file, look for function theme_links. This is just to give you an idea. Please do not update / delete / modify this function. Ok? :)
In this function, you can see the following code (or atleast I think this is the relevant code)

    // Automatically add a class to each link and also to each LI
      if (isset($link['attributes']) && isset($link['attributes']['class'])) {
        $link['attributes']['class'] .= ' ' . $key;
        $class = $key;
      }
      else {
        $link['attributes']['class'] = $key;
        $class = $key;
      }
  

What this is doing is to add class parameters to each link which you can now style using CSS! Hooray!

What you want to do to add the icons
If you enable comments for nodes (say blog entries for example) - go ahead and add a blog entry and then publish it. Once published, look at the node and do a view page source... on it. Look for the following code

  <ul class="links inline">
<li class="first blog_usernames_blog"><a href="/drupal/blog/1" title="Readlatest blog entries." class="blog_usernames_blog">Author blog</a></li>

<li class="comment_add"><a href="/drupal/comment/reply/2#comment-form" title="Share your thoughts and opinions related to this posting."<strong> class="comment_add"</strong>>Add new comment</a></li>

<li class="subscriptions_add_blog"><a href="/drupal/subscriptions/add/blog/1" title="Receive an e-mail whenever a new entry is made to this author blog." class="subscriptions_add_blog">subscribe blog</a></li>
.
. //(many more links, depending on  your modules)
.</ul>  

So now to add an icon for Add new comment text, you can add this to your style sheet for the class="comment_add"

/**
*CSS Icons for various links
*/
.comment_add{
    padding-left: 20px; // so there is space on the left of the text for the image. 
	background:url(icons/comment_add.png)  no-repeat;
}

Note that the icons directory is within the relevant theme.. so its drupal/themes/yourtheme/icons/nameoftheicon.jpg

Keep adding this for every class you see at the top .. e.g. you can add the css for subscriptions_add_blog, subscriptions_add_node etc etc.

Hope this helps.

jwolf’s picture

you'll need to add an anchor ( "a" ) to each class to prevent the icons from doubling up.
for example:

.comment_comments a {
    padding-left: 20px;
    background:url(icons/comment.png)  no-repeat;
}

.comment_add a {
    padding-left: 20px;
    background:url(icons/comment_add.png)  no-repeat;
}

.node_read_more a {
    padding-left: 20px;
    background:url(icons/page_white_go.png)  no-repeat;
}

... and so on.

mariagwyn’s picture

What if I want to replace the TEXT itself? I am using 5.1, and would like to not just add an icon, but replace the text using the template.php file. Specifically the book 'printer-friendly page' text.

Thanks, Maria

Baker’s picture

I'm sure there's a way to do it in the CSS for each them, but if I can't do it there, I plan on hacking into each of the modules and replacing the text they send as a link with a space. Help anyone?

SteveK’s picture

check out the Custom links module. This will allow you to attach links based on the specified content types. ex: I want a "LISTEN TO ME" link on all my postcast content types.

---
I work here: Raincity Studios.com
I hang out here: Steve Krueger.com

Baker’s picture

...I wanted to do. I checked it out and either I didn't understand it, or it didn't do what I wanted done. I wanted some of the links with just icons, some just text, and some with icons and text.

I got the desired effect by hacking into each of the modules and deleting the text where I wanted only an icon. I'm almost there. Now I just have to figure out how to order the links to my liking and then find a way to put the Read More link at the end of the text as Continued...

Thanks to all who gave great advice here and thanks to SteveK for taking the time to help.

mrgoltra’s picture

luisfeng’s picture

Is there a easier way for drupal6?

-----------------------
Join the community!