Hey,

I recently upgraded to flag 2 beta 3 because of the anonymous user support. I integrated session API and I'm trying to allow users, both anonymous and authenticated, to flag themselves with either one of two flags to determine site language. (There ain't no f'in way my clients are ever going to translate string by string with standard locale interface - but if I can set a global 'flag' on one of two languages, I can configure views with some rules magic to only show certain CCK fields of nodes, one in language A or in language B depending on what is flagged).

I'm trying to manually place the link, so I guess this is a two part problem.

#1. Here is my code to manually place the link. I JUST integration session API so I'm still unsure how it handles the $user object but with that said I some hash'd value that generally gives it some temporary ID within GLOBALS['user'].

<?php
 
global $user;
$uid = $user->uid;
print
flag_create_link('global_flag', $uid);
?>

If this is wrong, or won't for anon users, should consider that as if it ties into problem 2.

#2. This code generates a link for anon users. When I click it, I get this message:


Access denied
You must have JavaScript and cookies enabled in your browser to flag content.
You are not authorized to access this page.

Why do you suppose I'd get this error? With the use case above, what do you suppose would be the best way to go forward?

Thanks!

Comments

quicksketch’s picture

If this is wrong, or won't for anon users, should consider that as if it ties into problem 2.

The existing Flag implementation will not work for the flagging of anonymous users. Flag depends on a single unique numeric ID to track what content has been flagged. Since ALL anonymous users have an ID of 0, they cannot be flagged because they do not have a unique ID. Of course there is also the session ID, but because this is a different ID than UID, you'd have to make a completely different flag for anonymous users (similar to how "node" and "user" flags are different things right now, you'd need a dedicated flag type for "anonymous user").

Generally I think what you should do instead in this situation is use a profile field to store information about a user, rather than Flag.

ronincreative’s picture

Hey Quicksketch,

I think I'm going to try having users flagging a node, and using that node as the dedicated test for that system and see if that works. The prob is, my clients need anon users to be able to swap languages. I'll post back on the successes of this approach.

Thank you again, and always, for always responding promptly to issues in this queue.

ronincreative’s picture

So I tried using a node as the flag, but it's being problematic.

Anonymous Users can flag the node if they're viewing the node page, and the node's on the footer. However, if on say, my front page, I put:

<?php
 
$nid
= 174;
print
flag_create_link('language_flag', $nid);
?>

Anon users get sent to a page that says:

Access denied
You must have JavaScript and cookies enabled in your browser to flag content.
You are not authorized to access this page.

That code, in my page.tpl.php file, works if users are viewing /node/174 - but only on that page.

Any thoughts?

quicksketch’s picture

Anon users get sent to a page that says:

So you're implying that you can successfully flag content if the user is logged in? What's the URL of the page that anonymous users are sent to upon trying to click that link?

ronincreative’s picture

Yes, authenticated users can flag the link at any and all locations. Anonymous users can only flag the node when viewing the node itself.

The flag is called 'language_flat' of type 'node', and is a javascript toggle. The node ID being flagged is 174.

The code to create the link is placed on every page via page.tpl.php, and as mentioned above is:

<?php
$nid
= 174;
print
flag_create_link('language_flag', $nid);
?>

Auth users can successfully flag the node at any location. Anonymous users can only successfully flag if on the node itself /node/174. If they are not viewing the node itself, they are redirected to this URL:

http://www.bes.ronin-creative.com/flag/flag/language_flag/174?destinatio...

This ?destination= location changes based on what page the link is on.

You can see the actual link here: http://www.bes.ronin-creative.com. The flag link is up top, says 'flag estonian,' up top. This link works fine for auth users anywhere. If you go to /node/174 - the link will work for you anonymously, but only on that page.

Thanks!

quicksketch’s picture

This looks like it's because flag.js is not on the frontpage for anonymous users. Because the $scripts variable has already been printed out by the time you call flag_create_link(), the necessary JS cannot be added to the page. I'd suggest creating a link in block or using hook_preprocess_page() to create your variable earlier in the page load. Or force flag.js to be added on every page by using drupal_add_js() in hook_init().

ronincreative’s picture

Status:Active» Fixed

@quicksketch - this works. Thanks!

Putting it in a block solved this issue. I'll just use blocktheme and AP position it.

Thanks again!

Marking as fixed.

Status:Fixed» Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

mooffie’s picture

People may be referred to this document, so two clarifications:

Or force flag.js to be added on every page by using drupal_add_js() in hook_init().

That won't quite help. Merely adding "flag.js" doesn't yet add some anonymous-related JavaScript variables (e.g. 'Drupal.settings.flag.anonymous' and 'Drupal.settings.flag.templates').

I'd suggest [...] or using hook_preprocess_page() to create your variable earlier in the page load

Unfortunaely, hook_preprocess_page() executes after template_preprocess_page(), so $scripts isn't affected (But you can regenerate it in your hook).

(Sorry: I'd have provided a solution but I don't currently have an elegant one.)

mooffie’s picture

(Sorry: I'd have provided a solution but I don't currently have an elegant one.)

I don't have the time to check that this indeed works, but here's something cute to try:

In your node.tpl.php (or MYTHEME_preprocess_node()) put:

<?php
  drupal_set_content
('flag_links', flag_create_link('bookmarks', $node->nid));
?>

Then, in your page.tpl.php, do:

<?php
 
print drupal_get_content('flag_links');
?>
jfarry’s picture

Version:6.x-2.0-beta3» 6.x-2.x-dev

hi guys,

I'm experiencing this problem also (in the current dev and 6x-2.0 beta5 versions). In my case though, I'm printing a list of flags in a view (that is loaded in a lightbox iframe), with an accompanying 'remove' button. I get the same error as above, with anon users being redirected to the login page:

Access denied
You must have JavaScript and cookies enabled in your browser to flag content.
You are not authorized to access this page.

In the case of using a view (of type Page) how can I implement your solution for #10 moofie? I've tried adding the PHP to node.tpl.php and then replacing the views_embed_view() on my page with the drupal_get_content() snippet above, but this isn't returning anything (apologies if that's not how it's meant to work and I've interpreted it the wrong way :D ).

For the moment, I've removed the "remove" button from anon users as they can still remove the flag by going to the page that they originally flagged it on.

Thank you for your time,
josh

jfarry’s picture

Status:Closed (fixed)» Active
marcoka’s picture

the disadvantage of #10 seems to be that if you are on a taxonomy page (overview) with 10 nodes, the flag is loaded 10 times. So if you decided to output it next to the breadcrumb you have 10 flag items.

kenorb’s picture

Category:support» bug

The same problem with: 6.x-2.0-beta5
Where's the solution?

JayKayAu’s picture

Ditto with 6.x-2.0-beta5

Is the problem simply that "flag.js" and the JS variables "Drupal.settings.flag.*" are not being loaded for anon? Or is it a greater problem than that?

kenorb’s picture

Any errors in javascript i.e.firebug console or inspector you see?
My problem was that I'd some conflicts with other modules which cause some javascript errors.

quicksketch’s picture

Title:Problems with Anonymous Users Flagging» Problems with Anonymous Users Flagging (JS variables not added to the page in time)
Category:bug» support

Is the problem simply that "flag.js" and the JS variables "Drupal.settings.flag.*" are not being loaded for anon? Or is it a greater problem than that?

This isn't a bug directly in Flag, its a problem with the way the original poster (and subsequent commenters) have been printing the flag links. Simply put, you need to output the flag links somewhere *before* page.tpl.php. Flag can't add the JS variables to the top of the page if the top of the page has already been printed out.

jfarry’s picture

Hmmm, please forgive my n00bness, but should flag.js be called via drupal_add_js()? And as that function is outside of the theme scope, should it be done via a custom module, or should it be added as an option to the original module?

quicksketch’s picture

Hmmm, please forgive my n00bness, but should flag.js be called via drupal_add_js()?

The flag.js file (and all other JS that needs to be added to the page) is added for you when you call flag_create_link() as long as you've called flag_create_link() before page.tpl.php (such as putting it in a block or in node.tpl.php). You probably shouldn't be trying to add flag.js to the page manually anyway because Flag requires more than just that file (it also requires JS settings) to be added to the page in order to function correctly.

quicksketch’s picture

Status:Active» Fixed

Status:Fixed» Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.