5

I'm using the Flags module for some functionality on my site. When a node is deleted, I need to check whether it's flagged or not and take additional action based on that. I've implemented hook_nodeapi() in my module to add my logic when a node is deleted. However, it seems I'm unable to check whether the node is flagged or not... I always get FALSE when I check, even when I know that node was in fact flagged. My code so far is:

// check if this node is flagged for renewal
$flag = flag_get_flag('machine_name_of_my_flag');

if($flag && $flag->is_flagged($node->nid)) {
   // add my custom logic here
}

This code is definitely run when the node is deleted. The $flag is found correctly, but $flag->is_flagged($node->nide) always returns false. I based this code on the documentation for the Flag API. I'm not sure why the is_flagged check is always returning false. Is it something to do with the delete action? When I debug this code in xDebug, I can see all the properties of the node object, but I can't find out if it's flagged or not.

4

That code works, but if the module that implements hook_nodeapi() is executed before Flag; differently, it will be executed after flag_nodeapi(), which removes any reference to the deleted node from the database tables used by Flag.

case 'delete':
  foreach (flag_get_flags('node') as $flag) {
    // If the flag is being tracked by translation set and the node is part
    // of a translation set, don't delete the flagging record.
    // Instead, data will be updated in the 'translation_change' op, below.
    if (!$flag->i18n || empty($node->tnid)) {
      db_query("DELETE FROM {flag_content} WHERE fid = %d AND content_id = %d", $flag->fid, $node->nid);
      db_query("DELETE FROM {flag_counts} WHERE fid = %d AND content_id = %d", $flag->fid, $node->nid);
    }
  }
  break;

If the short name of the module starts with any letter after the f, then that module needs to change its weight to a number lower than 0; it is usually done in the implementation of hook_install(), or hook_update_N().

db_query("UPDATE {system} SET weight = %d WHERE name = 'mymodule' AND type = 'module', -1);

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.