Placing a flag link on a 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).
Example 1
For nodes
<?php print flag_create_link('bookmarks', $node->nid); ?>
(Put this, for example, in 'node.tpl.php'.)
For comments
<?php print flag_create_link('my_comments', $comment->cid); ?>
(Put this, for example, in 'comment.tpl.php'.)
For paragraph items:
<?php print flag_create_link('paragraph_flag_name', $paragraphs_item->item_id); ?>
(Put this, for example, in 'paragraphs-item.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).
Example 2: For users
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);
?>Drupal 8+
See flag.module file function flag_entity_view()
$build['follow_name'] = [
'#lazy_builder' => [
'flag.link_builder:build',
[
$entity->getEntityTypeId(),
$entity->id(),
'flag_name',
],
],
'#create_placeholder' => TRUE,
];
Equivalent without using #lazy_builder:
$build['follow_name'] = \Drupal::service('flag.link_builder')
->build($entity->getEntityTypeId(), $entity->id(), 'flag_name');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.
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion