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

CommentFileSizeAuthor
#12 12-10-2013 18-22-27.jpg6.36 KBmarycka9

Comments

mooffie’s picture

First, 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.

1) Using an img tag as the actual message. No good, because all the special characters are escaped.

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'.

2) Adding in the "flag_create_link" function, before realizing that that's for Drupal6.

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.

leenwebb’s picture

Ah, 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:

if ($action == 'flag'){$img_tag = '<img src="/themes/mytheme/images/add_to_favorites.png">';}
if ($action == 'unflag'){$img_tag = '<img src="/themes/mytheme/images/remove_from_favorites.png">';}

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!

mooffie’s picture

Status: Active » Fixed

Can I suggest that the handbook pages have some sort of version number included in them?

That 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.

===

if ($action == 'flag'){$img_tag = '<img src="/themes/mytheme/images/add_to_favorites.png">';}
if ($action == 'unflag'){$img_tag = '<img src="/themes/mytheme/images/remove_from_favorites.png">';}

Or you could do something along of:

if (file_exists(..... $flag->name . '-' . $action . '.png')) {
  $img_tag = '<img src="....... $flag->name . '-' . $action . '.png">';
}

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.

mooffie’s picture

I 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.

leenwebb’s picture

Thanks 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.

mooffie’s picture

[...] Please don't switch to two dashes in the name
my favorite thing [...] is how I don't have to relearn paradigms for every new module

Thanks 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.

Everything else I've done in Drupal uses single dashes

(This changes with Views 2.x, which uses two dashes.)

Laurentvw’s picture

subscribing

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

solona’s picture

HAHA! 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!

glitz’s picture

just 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

sp09’s picture

I've tried both ways but couldn't get an image to show up for drupal 6.

marycka9’s picture

StatusFileSize
new6.36 KB

I 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'] = 'Only local images are allowed.';
}

in path images themes copy images with name lag-[flag-name]-off.png и flag-[flag-name]-on.png

Work!

wOOge’s picture

Issue summary: View changes

Using @marycka9 's code as inspiration, here is how to do it in Twitter Bootstrap:


// Function to enable css glyphicon flags instead of text flags
function THEMENAME_preprocess_flag(&$vars) {
	if ($vars['action'] == 'flag') {
	//Flag Off
	$csselement = '<div class="glyphicon glyphicon-unchecked"></div>';
	} else {
	//Flag On
	$csselement = '<div class="glyphicon glyphicon-check"></div>';
	}
	$vars['link_text'] = $csselement;
}