Having a "Please login 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 login" link instead.
Showing the link under the node
To show a link under a node we need to implemenet 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.)
<?php
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('Login to bookmark'),
'href' => 'user/login',
'query' => drupal_get_destination(),
),
);
}
}
}
}
?>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:
<?php
global $user;
if (!$user->uid) {
print l(t('Login 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":
<?php
function theme_views_handle_field_flag_ops_bookmarks_ops($fields, $field, $data) {
global $user;
if (!$user->uid) {
return l(t('Login 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 "Login 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:
<?php
return array(
'flag-anon' => array(
'title' => t('Login to bookmark'),
'href' => 'user/login',
'query' => drupal_get_destination(),
),
);
?>We can turn it into:
<?php
$message = t('@count users have bookmarked this; Please login to bookmark', array('@count' => $flag->get_count($node->nid)));
return array(
'flag-anon' => array(
'title' => $message,
'href' => 'user/login',
'query' => drupal_get_destination(),
),
);
?>
Comments
Any idea how you would add such a link to comments?
Just for reference (D6)
I managed to do it. Created a custom module and add the following hook (don't forget to change mymodule to whatever your modules is:
function mymodule_link($type, $node) {global $user;
if ($type == 'comment') {
if (!$user->uid) {
$flag = flag_get_flag('abuse') or die('no "abuse" flag defined');
return array(
'flag-anon' => array(
'title' => t('login to report'),
'href' => 'user/login',
'query' => drupal_get_destination(),
'attributes' => array('title' => t('Login to report this comment as offensive')),
),
);
}
}
}