I am trying to charge points for flagging a node with Rules in PHP, an I am using the following code.

 //Get variables and load user.
$uid = [userpoints-transaction:user:uid];
$account = user_load($uid);

// Check the user has enough points.
if ($loaded_points > 10) {


  // Flag a node.
  $flag = flag_get_flag('purchase') or die('no "purchase" flag defined');
  $flag->flag('flag', $node->nid);
  // Search for the newly created flagging entity.
  $flag = flag_get_user_flags('node', $node->nid);

  foreach ($flag as $flagging_object) {
    $flagging_id = $flagging_object->flagging_id;

  // Load the entity found and create or update a field value.
  $flagging = entity_load_single('flagging', $flagging_id);
  $flagging->purchase['und'][0]['value'] = 'DONT KNOW DEFAULT VALUE OF FLAGGING';

  // Save the updated flagging entity object.
  entity_save('flagging', $flagging);


I don't know how to charge them 10 points and what the default value is when flagging a node.

1 Answer 1


When you simply want to grant 10 userpoints to a user after flagging a node, you're making things way to complicated! This can be done without custom coding at all. Here's what you have to do:

  1. Create a new rule.
  2. Add an event "A node has been flagged, under YOUR_FLAG".
  3. Add an action "Grant points to a user" and give it an amount of 10.
  4. Done!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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