1

How do I write a function to determine if the user is either the node author or admin?

if ( logged user is author || admin ) {
  // …
}

It is for the node template.

2
  • Which template file do you want to alter? Commented Mar 6, 2012 at 9:20
  • Based on the title, this question could be one of three things. "How does boolean logic work (in PHP)?", "How do I determine if the user is also the author?", "How do I determine if the user is an admin?". Reading the question removes the first option. Downvoted for two questions in one, and a title that doesn't reflect the question well.
    – Letharion
    Commented Mar 6, 2012 at 9:53

3 Answers 3

5

Put this to template.php file of your theme:

function YOUR_THEME_NAME_preprocess_node(&$variables) {
  global $user;

  if(in_array('administrator', $user->roles) || $variables['node']->uid == $user->uid) {
    $variables['is_admin_or_author'] = TRUE;
  }
  else {
    $variables['is_admin_or_author'] = FALSE;
  }
}

then in your node.tpl.php or node--your-content-type.tpl.php you'll have a variable $is_admin_or_author with either TRUE or FALSE value.

1
  • This is a good answer, assuming that the question was about the node template. Commented Mar 6, 2012 at 9:51
5

Instead of checking if the user is the author or admin, it is recommended to check for permissions to perform a certain action. That will make your system more flexible and you're fully taking advantage of the permission system already built into Drupal.

For instance, if you want to show/hide something depending on the permission to edit the current node, you can use this (assuming you're working with node.tpl.php):

function YOUR_THEME_NAME_preprocess_node(&$variables) {
  if (node_access('update', $variables['node'])) {
    $variables['permission_to_edit'] = TRUE;
  }
  else {
    $variables['permission_to_edit'] = FALSE;
  }
}

Now you can use the following in your node.tpl.php:

if ($permission_to_edit) {
  print "You may edit this node!";
}
4
  • thanks marc, its useful idea, but its for flag module, to show something even when they aren't allowed to bookmark, so this won't work
    – devric
    Commented Mar 6, 2012 at 13:14
  • 1
    Of course this is just an example, there may be other permissions you can check with user_access that are more appropriate for your situation. Flag module itself also has functions to check certain permissions (see api.lullabot.com/file/contrib/flag/flag.module/7). Anyway, the most important message is this: I would always prefer checking permissions over checking user IDs or roles. Commented Mar 6, 2012 at 14:07
  • This is the Drupal way: You never check if the user has a specific role, but if the user has a specific permission. The reason is maintainability of the code: Suppose you add more roles, you don't need to rewrite all the if-statements because the code would always be if (user_access('get hamburgers')) { }.
    – avpaderno
    Commented Mar 6, 2012 at 14:34
  • well i checked off the ability to flag if their are either author/admin/anonymous, which create flag link will return null. i thought it would be easier this way, to check if they are logged/admin/author than show something else
    – devric
    Commented Mar 8, 2012 at 1:08
1
global $user;

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

You can also check for user roles by adding in_array('administrator', array_values($user->roles)) to the if statement if you had a seperate role (webmaster for example).

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.