I'm using flags on my site as a way of filtering content through views, when a user views a certain node content type, I have a rule setup which resets the value of my flag and sets it against that user for that node, the views will reference this information and change their output accordingly.

The problem I have at the moment is what happens when anonymous users enter the site for the first time, they have no flag set, and thus none of the views show anything, until they travel to one of these special nodes.

What I'm looking for is: is it possible to set a flag to a default value, so that everyone has this node tagged until it gets changed?

1 Answer 1

  • You need to use another flag to identify anonymous visits.
  • Flag value is always boolean , true or false. So you cannot have a default value to identify whether it's visited by anonymous.

What you can do is use a rule

  • Check visitor is anonymous
  • If yes set a flag called "anonymous_visit" to that particular node
  • Use above information in your views to display nodes

Above rule sets flag on view of a node, If you want to add flag by default to a content then your rule event should be on node create add xx flag to node...

You can also use flag api with node api hooks to achieve above.. See flag api below example..

Flagging or Unflagging an item You use the $flag->flag() method to either flag or unflag an item. Example:

$flag = flag_get_flag('bookmarks') or die('no "bookmarks" flag defined');
// Flag node #456:
$flag->flag('flag', 456); 
// Unflag node #456:
$flag->flag('unflag', 456);
The signature of this $this->flag() method is:

function flag($action, $content_id, $account = NULL, $skip_permission_check = FALSE)
  • At the moment my system already sets the flag whether or not they are logged in, but I'm looking for something that I can trigger the moment they first arrive at the site. What rule event would you suggest using to 'check visitor is anonymous'? Mar 18, 2014 at 12:35
  • User visits a page or a node ..
    – Anil Sagar
    Mar 18, 2014 at 14:03
  • Theres an event for when a user travels to a node, but unless I'm missing the obvious, there doesn't appear to be one for other types of pages. I've managed to set up a basic working example although I'm a little afraid it's very archaic way of doing it. I've set up the rule event 'When Drupal is initialised' and then set up conditions to check if the user has any node flagged, if not, it retrieves the entity by id and sets the flag for them. It appears to work, but I'm concerned the 'Drupal is initialised' is going to run all the time Mar 18, 2014 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.