1

I have a content type 'XYZ' for which the user has created nodes. Is there any way I can use to get the node ID of all the nodes created or deleted by user at the time of logout?

I am using hook_user_logout(), but I am not able to get the node IDs for the node created by the user.

3 Answers 3

2

hook_user_logout() is probably called only when the user uses user/logout to log out. If users don't log out all the times after they used the site, that hook is probably not going to be invoked. (For example, I have never logged out from Drupal.org, and I log in back once my session expires for the browser I am using.)
Furthermore, once a node is deleted, there is no track of it in the database, and since hook_user_logout() is going to be eventually invoked when it is too late, it is not going to find any information about the nodes deleted by the user.

I would rather use hook_node_insert() and hook_node_delete() to collect the data that will be used from another hook. For example, the following code would work.

function mymodule_node_insert($node) {
  $_SESSION['created'][$node->nid] = TRUE;
}

function mymodule_node_delete($node) {
  $_SESSION['deleted'][$node->nid] = TRUE;
}

If that is helpful information, instead of TRUE you can use REQUEST_TIME; in that way, you would log when the user created or deleted a node.

Eventually, you can use a database table instead of a session variable, but in that case you should clean it after the user logged out, if you want to reset the information you collected once the user logs out. With the code I shown, you would lose the information when the session expires for the user, so it is probably preferable for you to store the information in the database.

The method suggested by the other users doesn't work when the user creates a node and deletes it. Counting the number of nodes associated with a user ID doesn't include the nodes that have been deleted before the user logged out. Also, since users with the permission of administering nodes can associate a node with a user different from the user who created it, counting the nodes associated with a user ID is going to give wrong information.

screenshot

1

Like Nikhil M said, you can use EntityFieldQuery.

But I think it would be more efficient to use db_select in this case since no fields are manipulated.

It will look like something like this

function hook_user_logout($account) {
  $nids = db_select('node', 'n')
    ->fields('n', array('nid'))
    ->condition('n.uid', $account->uid)
    ->execute()
    ->fetchCol();

  if (isset($nids)) {
    //$user_nodes = entity_load('node', $nids);
 }
}
2
  • This doesn't work for deleted nodes, which are not even counted as nodes created by the user. The OP is asking "Is there any way I can use to get the node ID of all the nodes created or deleted by user at the time of logout?"
    – apaderno
    Aug 9, 2013 at 9:26
  • I just read your answer, you're right, I didn't pay attention to the deleted node.
    – rydje
    Aug 9, 2013 at 10:04
0

You could try EntityFieldQuery

function hook_user_logout($account) {
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'node')
      ->entityCondition('bundle', 'XYZ', '=')
      ->propertyCondition('uid', $account->uid);
    $result = $query->execute();

    if (isset($result['node'])) {
        $news_items_nids = array_keys($result['node']);
        //$news_items = entity_load('node', $news_items_nids);
        // Do Whatever....
     }

}

Give it a try and let me know the results.

2
  • k.will try and let u know if it works..
    – harshal
    Aug 9, 2013 at 8:01
  • this gives me nids created by the user but as mentioned by @kiamlaluno a problem may occur when nodes are deleted.anyways thanks.. Also I should have mentioned regarding deleting the node in the question.
    – harshal
    Aug 9, 2013 at 11:06

Your Answer

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

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