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.
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).
<?php print flag_create_link('bookmarks', $node->nid); ?>
(Put this, for example, in 'node.tpl.php'.)
<?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):
<?php print flag_create_link('fools', $account->uid); ?>
$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
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)
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.