I have 2 node types - questions as parents and answers as referencing nodes. I would like to display flags only for nodes created as an answer to a given question. This should be only selectable by the author, not everyone.(same as stackoverflow best answer) So far I found out that I can manually place in node-answer.tpl.php the link using:

<?php print flag_create_link('select_answer', $node->nid); ?>

This will print the flag on every answer node, but I would like it to be selectable only by the author of this particular question. (basically it is the same functionality as stackoverflow - author can select best answer) There is an API function flag_create_link($flag_name, $content_id), so I need to set $content_id to point to all referenced answers for a given question.

ANy idea how can I do it? Any advice appreciated.

2 Answers 2


Are they 2 nodes or 2 node types? If types, just create 2 separate .tpl files for them (node--type1.tpl.php and node--type2.tpl.php).

If they are the same content type, you can check that the node reference exists inside node.tpl.php like so:

Edit: code below now only prints the link if the current user is also the author of the node.

global $user;
if ($user->uid == $node->uid && isset($node->field_my_reference_field_name[LANGUAGE_NONE)) {
  print flag_create_link('select_answer', $node->nid);
  • Of course node-types, sorry for the mistake. Question and answers posted below by people. I would like the author to select best answer by selecting a flag. The code you gave me will still be selectable by everyone, not only the author.
    – Vonder
    Jan 21, 2012 at 17:44
  • See new code in post above. Jan 22, 2012 at 16:41
  • excelent, thannk you
    – Vonder
    Jan 22, 2012 at 17:05

The module Answers may provide the functionality you are looking for. Also check out the related modules, Best answer and Related answers (listed on the Answers module page). Do not load Answers Userpoints. It is missing dependencies that have no releases at this time.

  • I know this module, I use it now but Best Answer doesn't work so I decided to use Rules and Userpoints. Works much better. Evntually I managed to acomplish that by writing custom code - check logged user id if it is equal to question author - display flag, if not - do nothing:)\
    – Vonder
    Jan 22, 2012 at 15:21
  • 1
    What is not working in "Best Answers'? I have played with it, but have not implemented it yet. What goes wrong?
    – Ashlar
    Jan 22, 2012 at 17:15
  • For me it doesnt work, I tried both on existing installation and brand new. You can find a topic here - drupal.org/node/1363704.
    – Vonder
    Jan 23, 2012 at 19:17
  • I'm using Drupal 7, that may be the difference.
    – Ashlar
    Jan 24, 2012 at 0:52

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.