1

I'm using hook_comment_view($comment) and I'd like to confer the uid from the comment ($comment->uid) with the uid from the node on which is commented. I thought of $node->uid, but I don't know how I can define $node.

Any suggestions?

  • Did you dpm() the $comment to see if the node is there? I can't remember. – mpdonadio Feb 8 '14 at 16:45
  • @MPD: Thanks to your dpm() hint, I found out that I could load the nid via $comment. – Jeroen Feb 8 '14 at 17:05
1

I could load the $nid via the comment and confer the uid's like this:

function mymodule_comment_view($comment) {
 $nid = $comment->nid;
 $node = node_load($nid);
 if ($comment->uid == $node->uid){
    //do something
 }
}
  • 1
    You can likely simplify this, too, by getting the $node->uid via a query. Something like $uid = db_query('SELECT n.uid FROM {node} n INNER JOIN {comment} c ON c.nid = n.nid WHERE c.cid = :cid', array(':cid' => $comment->cid))->fetchField(); would be a start. – mpdonadio Feb 8 '14 at 17:21
  • @MPD: Is that a faster way? I'm no expert in this. – Jeroen Feb 8 '14 at 17:25
  • Full node loads can be expensive, especially if you have a lot of fields. It is worth spending a little time with it, even if it is just to get used to doing db_queries for simple things. – mpdonadio Feb 8 '14 at 17:29
  • @MPD: Indeed, running the code seems to go faster now. Thanks for your advice! – Jeroen Feb 8 '14 at 17:47

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.