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?
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityThis 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);
}
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.
Query took 0.0002 sec
.
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.