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(),
),
);
Comments
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:
I managed to make this work
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:
Then, i can call this function from a theme template when i want to by using
The question is, is this the Drupal way of doing things?
I'm using the Flags API 2.0,
I'm using the Flags API 2.0, and ended up doing this:
Note the extra $flag argument to specify the desired flag name, e.g. "bookmarks".
module
You can also use the Flag anon module.
Thanks.
I added a link to that module. The "Custom Links" module lacks some extremely basic features, so your module seems a better choice.
Flag showing repeating twice
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?
How do you display the flag count in views
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!!
Drupal 7 method that worked!
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.
Hi, Is there a way to add to
Hi,
Is there a way to add to bookmark after the redirection ?
Thanks !
http://www.joel-rotelli.info
http://twitter.com/joelrotelli
Brilliant! Thanks so much for
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:
Experience the fascination of a tourist; Feel the comfort of the local
The Local Tourist
D7 method that worked
@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.
Is there a Drupal 7 way to do
Is there a Drupal 7 way to do this through a function?