Hey all,

I would like to add content to a node based on the node author's role. For example, if the author is an administrator, I'd like to add a link at the bottom of the node. I've Googled & perused the forums for the past hour, but nothing I've found has worked so far. Any suggestions?

thanks!

Comments

Bricks and Clicks Marketing’s picture

Someone must have had to do something like note that a node was composed by a website staffmember or something...

Arp Laszlo
bricksandclicks.marketing
design / theming / development / consulting

WorldFallz’s picture

You could do it with a block or by editing the node.tpl.php file directly.

Something like this should work in a block:

if (arg(0) == 'node' && is_numeric(arg(1)) && is_null(arg(2))) {
  $node = node_load(arg(1));
  $author = user_load(array('uid' => $node->uid));
  if (in_array('administrator', $author->roles)) {
    print "This author is an administrator.";
  }
}

This assumes you have a role named "administrator", you'll need to alter as necessary.

Bricks and Clicks Marketing’s picture

That's exactly what I was looking for. Is this something I could have intuited from the API documentation? (being a relative php noob, that is)

Arp Laszlo
bricksandclicks.marketing
design / theming / development / consulting

WorldFallz’s picture

you're welcome ;-)

Being familiar with the api is definitely helpful-- knowing that user_load(), node_load(), and arg() exist and what they do will help you when you think of something you'd like to implement. Whenever I'm trying to figure out how to do something I go to api.drupal.org first and see if there's anything there I can use-- then I go to php.net. Nine of ten times I find something useful in the drupal api.

Bricks and Clicks Marketing’s picture

on learning Drupal & php better. I very much appreciate the help :-)

Arp Laszlo
bricksandclicks.marketing
design / theming / development / consulting

Bricks and Clicks Marketing’s picture

I FINALLY got around to using this and it works perfectly! Of course, you would not have recommended it if it did not ;-)

I now have a related question - how can I alter this to print something if someone is *not* an administrator? IE, if someone is an admin, print X; if someone is not an admin, print Y. Thanks!

Arp Laszlo
bricksandclicks.marketing
design / theming / development / consulting

jscoble’s picture

Modifying the above code example you would complete the if/else evaluation so that something is done on the 'else' condition:

<?php
  if (in_array('administrator', $author->roles)) {
    print "This author is an administrator.";
  }
  else {
    print "This author is not an administrator.";
  }
?>