Hi all,
Is it possible to make the "Flag this"/"Unflag this" links images? Things I have tried:
1) Using an img tag as the actual message. No good, because all the special characters are escaped. I tried un-escaping them in my node.tpl, which works fine as long as the link is never clicked (and then the JS takes effect and displays the escaped characters again.)
2) Adding in the "flag_create_link" function, before realizing that that's for Drupal6.
3) Glaring at the screen.
None of these things have worked! I know I can add an image in with CSS, but I'd really like the link itself to be an image, all button-like.
Thoughts? Thanks!
Eileen
Comments
Comment #1
mooffie commentedFirst, upgrade to the "dev" version. The "beta3" you're using is very old (we will soon release "beta4").
You'll find a Theming guide in the handbook.
Open 'theme/flag.tpl.php' in an editor. You'll find that the text of the link is wrapped in
strip_tags(...). That's the reason you can't embed HTML in labels. You can remove this wrapping if you want to be able to use HTML directly in labels.I've just opened an issue, #304369: Allow HTML in the flag text?
But instead of removing the "strip_tags" you can type the "{img src=...}" directly into the template. This is better, because the label may appear in other places in Drupal, and you wouldn't want to mess it up with markup.
Of course, you'll want to copy 'flag.tpl.php' into 'flag-favorites.tpl.php' (replace 'favorites' with the machine-name of your flag), because you wouldn't want all flags to look the same. Copy that file into your theme folder. The instructions are in 'theme/README.txt'.
It's for D5 as well, it's just that it isn't in the old "beta3". But this function isn't used for theming: it's used for placing. The handbook explains this as well.
Comment #2
leenwebb commentedAh, perfect! (I didn't realize I was so unfashionable with my beta3! :)
I created a flag-favorites.tpl.php and everything works just as I'd hoped. If anyone else comes across this thread, here's what I did to make my images show up:
I added in that code to my template. Then I displayed $img_tag in where the $link_text used to be. Voila!
---
Can I suggest that the handbook pages have some sort of version number included in them? Right now they just say "Drupal 5.x" or "6.x" and given that the beta3 version is the "recommended for 5", I'm not sure how I would ever figure out on my own that I should upgrade to the dev version. (I thought that the general rule-of-thumb was "Don't take dev versions unless you're willing to deal with craziness", so it doesn't ever occur to me that the documentation would apply to to the newest dev snapshot instead of the Official Release.)
Thanks for your help -- Flag is just what I was looking for. I love that every time I think, "Hmm, I wonder how I can do X in Drupal", I search around and find that someone has already made a brilliant version of X!
Comment #3
mooffie commentedThat won't be needed: beta4 is about to be released.
Our handbook was revamped only very lately. It wasn't yet "advertized". It's kind of secret.
For the short period of time till beta4 is released, we'll rely on the axiom "Users never read documentation", so no confusion will arise.
Of course, you're right in what you're saying. Thanks for the comments. I hope such a state won't occur again in the future. We should release more often.
===
Or you could do something along of:
Then you wouldn't have to update the template for every flag.
I think I'll put the recipe to this in the handbook. (And I think we will remove the strip_tag() so it'd be possible to do this in a preprocess function and not alter the template.
Comment #4
mooffie commentedI put the recipe in the handbook:
How to show images instead of text
(But note the message at the bottom of that page: you'll have to upgrade your 'dev'.)
In addition, it might be that, in the final release, we'll use two dashes in the template name: flag--bookmarks.tpl.php instead of flag-bookmarks.tpl.php.
Comment #5
leenwebb commentedThanks for all your help mooffie -- My flag images are working just how I'd hoped!
If you're taking votes from the only-marginally-involved... Please don't switch to two dashes in the name! Everything else I've done in Drupal uses single dashes ( just like flag-bookmarks.tpl.php and node-news.tpl.php), and one of my favorite things about working in Drupal is how I don't have to relearn paradigms for every new module I install.
Comment #6
mooffie commentedThanks for the feedback. It's important that we know what users prefer.
it now seems that we'll switch from "flag-bookmark.tpl.php" to "flag-link-bookmark.tpl.php". I'm not very happy about this, about lengthening the file name.
(This changes with Views 2.x, which uses two dashes.)
Comment #7
Laurentvw commentedsubscribing
Comment #8
Anonymous (not verified) commentedAutomatically closed -- issue fixed for two weeks with no activity.
Comment #9
solona commentedHAHA! I've tried glaring at the screen too, it never works.
Thanks so much for this post, it was very helpful. Got an image working!
Comment #10
glitz commentedjust wondering the steps you took to get it working.
i am still having trouble replacing the default flag link, with a custom image...
thanks!
chris
Comment #11
sp09 commentedI've tried both ways but couldn't get an image to show up for drupal 6.
Comment #12
marycka9 commentedI made so:
(senks http://odrupal.ru/drunews/snippety/zamenit-ssylku-modulya-flag-na-izobra... )
1. Copy flag.tpl.php in folder theme;
2. In template.php past function:
function framework_preprocess_flag(&$vars) {
';
$image_file = path_to_theme() . '/images/flag-' . $vars['flag']->name . '-' . ($vars['action'] == 'flag' ? 'off' : 'on') . '.png';
// uncomment with testing
//drupal_set_message("Поиск изображения '$image_file'...");
$vars['link_text'] = '
}
in path images themes copy images with name lag-[flag-name]-off.png и flag-[flag-name]-on.png
Work!
Comment #13
wOOge commentedUsing @marycka9 's code as inspiration, here is how to do it in Twitter Bootstrap: