TransWikia.com

Set node to flagged by default

Drupal Answers Asked by Andrew Morris on November 28, 2021

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?

One Answer

  • 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:

<?php
$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:

<?php
function flag($action, $content_id, $account = NULL, $skip_permission_check = FALSE)
?>

Answered by Anil Sagar on November 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP