Last updated October 9, 2012. Created by chellman on August 14, 2008.
Edited by forestmars, joachim, tim.foxell, julien.reulos. Log in to edit this page.
Flagging and unflagging content is usually done by clicking links. These links are known as "flag links".
The Flag module puts these links for you — for your users — in certain places. For example, for nodes, links are shown beneath the node body in the area where all other node links are usually shown (e.g., beside the "Add new comment" link).
Sometimes this doesn't suit you.
Sometimes this doesn't work in the specific theme you use.
Sometimes you just realized there's no native way to put a flag in a view.
Sometimes you want to place the link in quite a different place.
In these cases, you'll want to place the link manually on the page.
To do this, follow two steps:
- Locate the template file in your theme directory (siteroot/sites/all/themes/mytheme) where you want to put the link (e.g. 'node.tpl.php' or 'user-profile.tpl.php'), and add to it a call to
flag_create_link, as described below, in "Using flag_create_link()". - If all works to your liking, instruct the Flag module to not show its own link. (This step is optional: it's possible to have two or more identical flag links on a page.) Do this by going to the flag edit screen and ticking off all the checkboxes in the "Display options" section.
Using flag_create_link()
Once you tell the Flag module not to print the flag links, the responsibility for printing these links falls on your shoulders. Don't worry, it's easy. You need to call flag_create_link() from your template file(s). This function gets two arguments: the flag name, and the ID of the object to flag. Note: By "flag name" we mean the flag's machine-name (as appears on its settings page).
Examples:
For nodes:
<?php print flag_create_link('bookmarks', $node->nid); ?>
(Put this, for example, in 'node.tpl.php'.)
For users:
<?php print flag_create_link('my_lovers', $node->uid); ?>
(Put this, for example, in 'node.tpl.php'; $node->uid stands for the node's author.).
In the above example, 'my_lovers' is the name of an existing user flag. You can't flag users using a node flag (and vice versa).
For users (another example):
Drupal 6<?php print flag_create_link('fools', $account->uid); ?>
Drupal 7
<?php
$account = menu_get_object('user');
print flag_create_link('fools', $account->uid);
?>Where to put this? There are several possibilities:
- If you're using standard Drupal profile pages, put this, for example, in 'user-profile.tpl.php';
$account->uidstand for the user whose profile page is being viewed. - If you're using the Content Profile module, put this, for example, in 'content_profile-display-view.tpl.php' but change
$account->uidto$uid.
Troubleshooting
A problem you're likely to encounter is that the link doesn't show. Here are some steps that will guide you in solving the problem:
- Are you editing the right template file? Type "blah blah" into your template and see if it shows up in your browser.
- Calling flag_create_link() doesn't skip the flag's permission checks. Your link will never show up if the module's default link doesn't show up either. Make sure you associate the flag with the node type you are working with. And make sure the user browsing the page has permission to use this flag. In other words: before complaining "flag_create_link() doesn't work for me!", make sure the default link does work.
- Under Flag access by content authorship- did you check the box for "Users may only flag content of others"? If you did, then you will not be able to see the flag of anything you authored.
- Do the variables you're passing to flag_create_link() indeed exist in the template? It's easy to find out: place
<?php print_r($variable_to_check); ?>in your template to see if some variable exist. - Calling flag_create_link() in 'page.tpl.php'? See the next section.
The 'page.tpl.php' problem (Drupal 6 only)
Note that a problem arises when putting the call to flag_create_link() in 'page.tpl.php'. At this late stage the JavaScript and CSS sections of the page (i.e., the $scripts and $styles variables) have been printed out already. So Flag's JavaScript (which enables AJAX links), whose inclusion is triggered by flag_create_link(), won't be included in the page.
This problem is specific to page.tpl.php under Drupal 6 (Drupal 7 doesn't have the problem). One solution is to put the call to flag_create_link() in a block instead. Here's another solution.
Comments
Flags on a field page views - Openpublish
Hi,
I tried using views to place flags on a page.
When creating a view, I don't find in the field list the flags I created.
I am using openpublish distro.
Anyone has an idea why? Or is it just not compliant with views?
Thanks
Hello, You probably need to
Hello,
You probably need to add a relation : flags in your view. Then you can add the flag link in your fields.
THanks!
Thanks for the tip! that worked out for me .
thanks
thanks
Open Source,Open Mind.
Drupal Services and Web Design
Drupal web design & developm
دروپال فارسی | Official Drupal Persian Community Support
I also tried to add flag link
I also tried to add flag link into my view using relationship, but the whole view was hidden after it. May be it is because of I attach my view using module_invoke? If so how can I add such link?
Skype: kaztur.ru
ICQ: 415875063
Phone: +7 917 871 09 85
If there a way to add a flag
If there a way to add a flag summary into the node in code. (ie "12 users have flagged this node")?I have done this in my summary view with one of the fields provided by the module, but ideally I would like to place this into the page itself.Thanks.Let this be a lesson to all those Drupal people out there. Read before you post comments!!!
How to add flag count to your node: http://drupal.org/node/305086#flag-counts
This is your life, and it's ending one minute at a time
Printing all flagging links at once...how?
<?php print flag_create_link('my_lovers', $node->uid); ?>This is used to create a specific flag link.
Is there a way to show all flagging links, instead of adding them one by one?
Thanks!
Rosamunda
Buenos Aires | Argentina
Flag link in Views display
How to create a flag link in Views page? I'm using flag_create_link() but it don't working :/
adding flags with 2 parameters
I'm creating a flag which is similar to the "bookmark" flag in that it associates a particular user with a particular content item.
Is there any way for me to add a flag allowing an admin to associate a particular with a particular nid in a certain flag? Sort of like allowing an admin to add a bookmark FOR a user - not letting the user add that bookmark by themselves. This is actually for designating leaders for a group - there is a group content type, and then I have a user flag which is used to flag nids of groups for which they are designated as leaders.
CCK flag field
Is there an option to use CCK for the flag field ? Original functionality with just adding checkbox to node form doesn't work well with CCK related modules.
Strony internetowe Ełk
Add flag to comments - snippet
I used this on my site and thought it might be useful to someone else.
Manually add comment flag:
<?php print flag_create_link('flag_name', $comment->cid); ?>Thanks!
This is great, why isn't this included in the instructions above? That sure would have been nice.
flag_create_link doesnt work
I am having this rather strange problem. flag_create_link doesnt returns any html for a custom theme i made.. but works fine when switching to other default themes. On debugging it seems that the hook_theme isnt getting invoked for some reason. A print statement i placed in the flag_theme doesnt prints anything in mycustom theme but does goes invoked from other themes.
The strange thing is it used to work fine untill a few days back before i migrated to a new server. I might have made some unknown changes in the transition but have no clues what it is . Any suggestions on what i might have got wrong would be greatly appreciated.
Thanks.
Using this in Panels
If you are for some strange reason needing to create this link in Panels while Context Keywords on enabled, the following code works:
<?phpprint flag_create_link('bookmarks', %node:nid);
?>
Brian Altenhofel
Anrhizan - high performance Drupal hosting
where does this code go? i
where does this code go? i have set up several flags for cck types. i then migrated them over to panels for formatting reasons, and i have all content listed the way i like except the "flag" link....i cant seem to find where, or how to add it back in??
thanks
nm got it thx
nm got it thx
Could you share it? Thanks!
Could you share it?
Thanks!
Please share how you did that
I have the same issue and would like to know how you put a flag this link on the panel.
install in a panel - CHECK THIS
This thread should contain a file which works like a module. Might want to start at the bottom and work your way up.
http://drupal.org/node/332956
Using in panel
I simply added the code snippet to a panel pane using the 'New Custom Content' which is available when adding new content.
I've included a screenshot.
http://screencast.com/t/bQlrHmu55
Joshua Needham
WebMob Development, LLC
(801)613-2778
Follow me on Twitter @JoshuaNeedham
Very nice
I was trying the above example without success and then I found your comment http://drupal.org/node/295383#comment-3334516. It's working perfectly. Now to style the flag link to something other than text.
Joshua Needham
WebMob Development, LLC
(801)613-2778
Follow me on Twitter @JoshuaNeedham
Flag View error
I am getting errors when I try and view flagged nodes in my profile. I have cloned the original flag view but am using a custom flag for a node. What should the views relationships and views arguments be?
Error message:
________________
user warning: Column 'uid' in on clause is ambiguous query: SELECT COUNT(*) FROM (SELECT DISTINCT node.nid AS nid...
________________
The error repeats twice
Beneath the error it says "This user has not yet bookmarked any content." and I know that I have flagged two nodes (error repeats twice).
Help please
How To: Use Flag on Panel Pages
Brian, that was a bit of genius.
I was having an issue with getting a bookmark flag onto a panel page. After tinkering with multiple solutions I found this to be the most effective and wish I had seen this fifteen threads ago.
To clarify the solution:
This takes advantage of Context, Context Keywords and Flags to render the flagging links on Panel pages. Note: If you are using custom node templates instead of Panels this is not what you're looking for.
1) Install Context, Context Keywords and Flag (sudo drush dl flag context context_keywords)
2) Enable all modules (sudo drush enable flag context context_keywords)
3) Create a new panel on a page
4) Select "New Custom Content"
5) Copy/paste the code snippet from below:
<?php print flag_create_link('bookmarks', %node:nid); ?>* Notice the %node:nid instead of $node->nid
6) Configure the input format to "PHP Code" (if you don't see that option available you will need to enable the php input formatter)
7) Ensure "Use Context Keywords" is enabled
8) Save/Update the panel, open up a node and look at the magic.
Todd W. Shaffer
Is it possible to put that
Is it possible to put that code
<?phpprint flag_create_link('bookmarks', $node->nid);
?>
http://MYDOMAIN/flag/flag/bookmarks/?destination=node/9&token=uV1O-KznB5_6iIokf90eO0xP5YhpMejle9dg6ayhH2s"
is that my mistake?
Please help me and sorry for my bad english.
has to be in the template
i tried creating a preprocessed template variable and it doesn't work. worked when i put it into the tpl file. hope this helps.
Does this work for Flag Term?
I have tried putting flag_create_link in my page-taxonomy.tpl file. I added the following code:
<?phpprint flag_create_link('like', $tid);
?>
but it gave:
An HTTP 404 error occurred
http://localhost/drupal/?q=flag/unflag&destination=taxonomy/term/74&toke...
I know nothing about coding so any help would be much appreciated. I am using drupal 7.9. I have also tried:
<?phpprint flag_create_link('like', $term->tid);
?>
and it gives the same result.
drop down list
I want it to display as a drop down list showing all types of flags I made instead of a standard link. How can I do this?
I am looking for the same
I am looking for the same type of thing too. And I am using panels. None of the above tricks worked for me.
using flag abuse in a block?
How about using it in a block?
i added it however i'm getting an error:
<?phpprint flag_create_link('bookmarks', $node->nid);
?>
Error:
Notice: Undefined index: regions a block_admin_configure_submit() (línia 496 de /home/example/public_html/modules/_block/block.admin.inc).
Warning: Invalid argument supplied for foreach() a block_admin_configure_submit() (línia 496 de /home/example/public_html/modules/_block/block.admin.inc).
Notice: Trying to get property of non-object a eval() (línia 30 de /home/example/public_html/modules/php/php.module(80) : eval()'d code).
Any ideas on how to show flag abuse link in blocks?
thanks
Blocks have trouble accessing
Blocks have trouble accessing the node so try loading it first.
<?phpif ( arg(0) == 'node' && is_numeric(arg(1)) && ! arg(2) ) {
$node = node_load(arg(1));
print flag_create_link('bookmarks', $node->nid);
}
?>
Working with D7 global user flag
I got this to work using a global user flag in Drupal 7.
This will show the flag link anywhere and it works (no 404)
<?phpglobal $user;
print flag_create_link('service_status_normal', $user->uid);
?>
I'm by no means a php coder so I don't guarantee this for all setups!
Drupal 7 manually generate user flag
The $user array is the profile of the logged in user so the above code would generate the flag link to yourself, not the user profile you were viewing. Drupal 7 doesn't load the old D6 $account array so you need to generate it manually. See the following.
<?php$account = menu_get_object('user'); // Creates a user array based on the user profile you are viewing.
print flag_create_link('user_flag', $account->uid); // Where user_flag is the name of the user flag you created.
?>
You can drop the "global $user;" all together from the above code unless you are doing a check to see if the logged in user is looking at their own profile.
CSS to do it
Using views to include the OPS (remove/add flag) link made it impossible to break it down and edit individual parts (link vs. text vs. whole package). For anyone else having as much trouble as I was at 6AM trying to use tokens to change the link to an image instead of text, here's a very simple thing to do:
1. Add the Flagging: Flag link field to the view and use whatever default display you want.
2. Make sure your custom image is on the server and your CSS file can reference it (as in, not in a private files directory with no permissions).
3. Add this to the CSS file of your choice:
a.flag.unflag-action {
content: url(../images/minus.png);
}
This keeps the link and gives you an image to click instead of the ugly text that probably didn't fit to begin with. The text will still show in the HTML but browsers will replace with the image.
Without me, it's just aweso.
you are a genius! I could
Thanks, but this works only in Google Chrome, how about Firefox? Nvm, got it to work by using css for firefox!
Flag Count
Just curious - is there a easier way to show a number besides the flag button. For eg. if there are 20 users currently following a blog article, is there a way we can showcase that easily. If he/she chooses to unfollow, the corresponding number changes to 19.
I got it working!
I got it working. My bad, should have done my homework before posting this question.