5

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?

4 Answers 4

3

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);
}
2

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.

1
  • this is the issues how to look up the node ID (nid) by user ID (uid) ?
    – radu c
    Mar 22, 2011 at 10:09
2
$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.

3
  • As I there is just on node content per user I don't think is necessarily to set also the type. I have tested the query's to see which one is faster and the result was the same Query took 0.0002 sec.
    – radu c
    Mar 22, 2011 at 11:03
  • Are you sure that users will only ever have a single node they created? I'd suggest to add the type condition, just to be sure.
    – Berdir
    Mar 22, 2011 at 18:05
  • In my case it is sure that they will have only one node created, because they have no right to create nodes, only to view and edit it.
    – radu c
    Mar 23, 2011 at 7:46
0

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.

4
  • 3
    db_select() is 30% slower then db_query() should only be used if there is a reason for it. Reasons include that you need paging/tablesort, have dynamic queries or need stuff like node_access.
    – Berdir
    Mar 22, 2011 at 11:58
  • That's interesting. Didn't know that. Do you have a link to some benchmarks for this 30% slowdown? I'd like to know more about this! Mar 22, 2011 at 15:40
  • 1
    See drupal.org/node/1067802. It's not surprising, SelectQuery is an OOP abstraction layer with lots of string mangling and many, many method calls on top of db_query()
    – Berdir
    Mar 22, 2011 at 18:04
  • Thanks for this. I will start my own question to take this a bit further. Mar 23, 2011 at 9:39

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.