1

I am creating a project that needs ONLY the author of a node being referenced AND the author of the referencing node to participate on comments on the referencing node, a sort of customer support Question/Answer system where ONLY the node author of the referencing node and the node author of the referenced node can participate in the comments section. I am implementing this with panels.

So far i can show the comment block only to the referencing node author (Question author) using the following code which checks to see if node-author-id = logged-in-user id:

global $user;
if (arg(0) == 'node' && is_numeric(arg(1))) {
  // Get the nid
  $nid = arg(1);

  // Load the node if you need to
  $node = node_load($nid);
}

if($node->uid == $user->uid){
return TRUE;
} else {
return FALSE;
}

How do i also display this comment box to the author of the node being referenced?

Please note: The Question Content type and Answer Content type are two different content types

Thanks.

ps: I know about the Answers module as well as the Question/Answer modules. Somehow the best answer submodule included with the Answers7.x-4.0-rc2 module is able to let the author of the question flag an answer referencing his question as the best answer

1 Answer 1

2

Database query would help you.

function my_module_get_author_by_nid($node_nid) {
  $author = '';
  if (is_numeric($node_nid) && $node_nid != 0) {
    $uid = db_select('node', 'n')
        ->fields('n', array('uid'))
        ->condition('nid', $node_nid, '=')
        ->execute()
        ->fetchField();
    return $uid;
  }
}

// And then a slight change in your code

    global $user;
    if (arg(0) == 'node' && is_numeric(arg(1))) {
      // Get the nid
      $nid = arg(1);

      // Load the node if you need to
      $node = node_load($nid);
    }

     // Get Reference Node Nid
    $reference_nid = $node->your_reference_field_name[$node->language][0]['target_id'];
    // Get Author for reference Node
   $reference_author =  my_module_get_author_by_nid($reference_nid);
    if($node->uid == $user->uid || $user->uid == $reference_author){
      return TRUE;
    } else {
      return FALSE;
    }
8
  • I'll try the code and mark your answer if it works. Thanks! Commented Jul 3, 2015 at 10:43
  • I tried the code but it only displays the pane to the author of the node being viewed, but not also to the author of the referenced node Commented Jul 3, 2015 at 11:49
  • you have to replace the reference field with your field machine name $reference_nid = $node->your_reference_field_name[$node->language][0]['target_id'];
    – arpitr
    Commented Jul 3, 2015 at 12:13
  • yes i did, my entity reference field machine name is: field_referenced_question Commented Jul 3, 2015 at 13:25
  • are you getting UID returned from the my_module_get_author_by_nid() ? Ensure you are getting referenced node nid by debugging $node.
    – arpitr
    Commented Jul 3, 2015 at 13:45

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.