Question

A user is the owner of only one node, and I want to redirect the user, at login time, to his only owned node.

How can I get the node content if I know only the logged-in user?

Was it helpful?

Solution

This is how I have do it:

function MYHOOK_user_login(&$edit, $account) {
    global $user;
    $nid = db_query('SELECT nid FROM {node} WHERE uid = :uid', array(':uid' => $user->uid))->fetchField();
    drupal_goto('node/' . $nid);
}

OTHER TIPS

Take a look at hook_user_login -> http://api.drupal.org/api/drupal/modules--user--user.api.php/function/hook_user_login/7. This is where you can add your own custom code to do things like look up the node ID (nid) by user ID (uid) and then issue a drupal_goto (http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_goto/7)

There's also the option of doing the customization with hook_form_alter to the login form which might be considered more of a D6 solution. You'll have to provide more details if these suggestions are not addressing your question.

$nid = db_query('SELECT nid FROM {node} WHERE uid = :uid AND type = :type', array(':uid' => $user->uid, ':type' => $node_type))->fetchField():

This will get you the nid.

Note: please disregard this answer (see the comments for why).


$nid = db_select('node', 'n')
  ->fields('n', array('nid'))
  ->condition('n.uid', $user->uid)
  ->range(0, 1)
  ->fetchField()

I'd be tempted to do it this way since it's more the 'Drupal' way, although the other suggestions are certainly valid. Note that I've put a limit on the SQL statement for safety.

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top