0

I'm trying to display the flag link only for the node author, and I'm using the following code:

global $user;
if ($node->uid == $user->uid) {
  //print your flag
}

The link doesn't appear.

Do you have any idea on what is causing this?

1
  • Welcome to Drupal Answers. Please provide additional information. Without seeing the function it is in and its context to your custom module, it is hard provide any answers.
    – Ashlar
    Commented Jan 23, 2012 at 0:17

2 Answers 2

2

You may use hook_flag_access(), as in the following code.

 function crystal_qa_flag_access($flag, $entity_id, $action, $account) {
   // Only the author of the question has right to use flag "Accepeted" on answers.
   if (($flag->name == 'accepted') && ($flag->content_type == 'node')  && ($node = node_load($entity_id))) {
     if ($node->type=='answer') {
       // The content type machine name must be answer.
       $question_nid = $node->field_answer['und']['0']['target_id'];
       $question = node_load($question_nid);
       return ($question->uid == $account->uid);
     }
   }
 }
1
  • This seems the right answer.
    – avpaderno
    Commented Dec 22, 2012 at 5:11
0

Perhaps by declaring $user to be a blank variable right before you use it, you're overwriting Drupal's variable?

1
  • The variable is not set to a blank variable, when using global $user.
    – avpaderno
    Commented Mar 24, 2012 at 22:25

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.