Having a "Please log in to flag" link
When anonymous users --or otherwise unprivileged users-- visit your site they don't see the flag link. For these users you may wish to show a "Please log in" link instead.
But first: if you don't know PHP...
If you don't know PHP you have two options:
- Use the Flag Anonymous module.
- Use the the Custom Links module. Here's a "tips and tricks" page for that module.
(Note: The rest of this page is a bit old. Probably more of the suggestions here could be replaced by contrib modules nowadays.)
Showing the link under the node
To show a link under a node we need to implement hook_link in a custom module. We'll assume the name of this module is "mymodule".
(This code works for both Drupal 6 and Drupal 5.)
function mymodule_link($type, $node) {
global $user;
if ($type == 'node') {
if (!$user->uid) {
$flag = flag_get_flag('bookmarks') or die('no "bookmarks" flag defined');
if ($flag->applies_to_content_object($node)) {
return array(
'flag-anon' => array(
'title' => t('Log in to bookmark'),
'href' => 'user/login',
'query' => drupal_get_destination(),
),
);
}
}
}
}
Drupal 7.x-3.x note: applies_to_content_object() was renamed applies_to_entity(). See the issue.
Instructions for Views
When we're styling the view as a table (or as fields), we need to theme the "Flag: Links" field. (This field is also knows as the "Ops" field; that's why you're going to see "ops" in the code below.)
Instructions for Views 2.x (Drupal 6)
Create a "views-view-field--VIEWNAME--ops.tpl.php" file, in your theme folder, containing:
global $user;
if (!$user->uid) {
print l(t('Log in to bookmark'), 'user/login', array('query' => drupal_get_destination()));
}
else {
print $output; // This prints the link.
}
(Note: to find out the exact name to give this file, visit the "Theme: Information" link. And make sure you click the "Rescan template files" button afterwards so Drupal notices this new file.)
Instructions for Views 1.x (Drupal 5)
Put the following in your "template.php":
function theme_views_handle_field_flag_ops_bookmarks_ops($fields, $field, $data) {
global $user;
if (!$user->uid) {
return l(t('Log in to bookmark'), 'user/login', array(), drupal_get_destination());
}
else {
return theme_views_handle_field($fields, $field, $data);
}
}
Note the "bookmarks" word in the function name. You should change it to the "machine-readable name" of the flag you're actually using.
Fancier messages
We used a dull "Log in to bookmark" message. We can turn it into a more exciting one by embedding in it the number of users who have bookmarked the node. We can use the $flag->get_count() method for this purpose. For example, in the hook_link example at the start of this page we had:
return array(
'flag-anon' => array(
'title' => t('Log in to bookmark'),
'href' => 'user/login',
'query' => drupal_get_destination(),
),
);
We can turn it into:
$message = t('@count users have bookmarked this; Please log in to bookmark', array('@count' => $flag->get_count($node->nid)));
return array(
'flag-anon' => array(
'title' => $message,
'href' => 'user/login',
'query' => drupal_get_destination(),
),
);