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:

  1. Use the Flag Anonymous module.
  2. 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(),
  ),
);

Comments

IbnDrupal’s picture

Any idea how you would add such a link to comments?

IbnDrupal’s picture

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')),
          ),
        );
    }
  }
}
Cousken’s picture

I managed to make this work without bigger problems, but this way the "Login to bookmark" ends up at the bottom at this page, not where i want it to be. To get control over where this is displayed, i instead created a function that looked like this:

<?php

function mymodule_name_login_to_flag($node) {
  global $user;
  if (!$user->uid) {
    $flag = flag_get_flag('bookmarks') or die('no "bookmarks" flag defined');
    if ($flag->applies_to_content_object($node)) {
      return l(t('Log in to save the page'), 'user/login');
    }
  }
}

?>

Then, i can call this function from a theme template when i want to by using

<?php

print skanegy_tweaks_login_to_flag($node);

?>

The question is, is this the Drupal way of doing things?

Anonymous’s picture

I'm using the Flags API 2.0, and ended up doing this:

function custom_login_to_flag($node, $flag) {
  global $user;
  if (!$user->uid) {
    $output = l(t('Login'), 'user/login');
  } else {
    $output = flag_create_link($flag, $node->nid);    
  }
 return $output;
}

Note the extra $flag argument to specify the desired flag name, e.g. "bookmarks".

snufkin’s picture

You can also use the Flag anon module.

mooffie’s picture

I added a link to that module. The "Custom Links" module lacks some extremely basic features, so your module seems a better choice.

Stomper’s picture

I have placed the flag link in my custom .tpl. I have done some basic theming on the link via CSS (border, background color).

I have noticed that the links are repeating twice, but only only set of the links is being themed according to the CSS. Something is causing the link to appear twice. I only want the themed links to appear.

Any tips?

Jessica A’s picture

How do you display the flag count to users when using a custom views.tpl file? I'm using a flag link as a views field but want anonymous users to see a flag count instead of 'Log in to bookmark'

This was explained for a custom module but not for the views.tpl. I'm using the following code from the example but am not proficient enough in php to figure out how to change 'Log in to bookmark' to the views count. Any help is greatly appreciated!!

<?php
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.
}
?>
InTheLyonsDen’s picture

Here's what worked for me without having to create a custom module.

I added a Code Field in Display Suite with the following code. The div was just to make it theme easy. I created a new code field for each flag type.

<div class="bookmark-flag">
 <?php
global $user;
if (!$user->uid) {
  print l(t('Log in to bookmark'), 'user/login', array('query' => drupal_get_destination()));
}
else {
  print flag_create_link('bookmarks', $entity->nid);
}
?>
</div>
joelrotelli’s picture

Hi,

Is there a way to add to bookmark after the redirection ?

Thanks !

thelocaltourist’s picture

Brilliant! Thanks so much for posting this!

If you're using Panels, create a Custom Content Pane and be sure to select "Use context keywords". Works like a charm!

EDIT: To use it in panels you have to change the code a slight bit:

global $user;
if (!$user->uid) {
  print l(t('Log in to bookmark'), 'user/login', array('query' => drupal_get_destination()));
}
else {
  print flag_create_link('bookmark', %node:nid);
}

Experience the fascination of a tourist; Feel the comfort of the local

The Local Tourist

djkentuckyham’s picture

@InTheLyonsDen
Hi. I was wondering if you could provide any more detail on on how you made it work. I have the default bookmarks flag working and attached to nodes. I also have Display Suite installed and running but obviously, I must be doing something wrong. I am uncertain how to tie your above to the flag that appears on the node. I checked the node option but doesn't see to want to work. Dp I need to allow/deny flag access for anon users? I would so. Any help would be appreciated.

-Mania-’s picture

Is there a Drupal 7 way to do this through a function?