Display content according to User Role AND Author Role

Last modified: July 9, 2008 - 08:03

by abramo

To display content within a node according to User Role AND Author Role - when for example User Role has to be "X" and at the same time Author Role has to be "A" - use the following snippet in your template for the Content Type in question:

<?php
global $user;
$author = user_load(array('uid' => $node->uid));

if ((
in_array('X', $user->roles)) && (in_array('A', $author->roles)))
{
print
"TRUE";
}

elseif ((
in_array('Y', $user->roles)) && (in_array('B', $author->roles)))
{
print
"FALSE";
}
?>

Also "OR" may be employed, if necessary - for example:

<?php

if (((in_array('X', $user->roles)) && (in_array('A', $author->roles))) or ((in_array('Y', $user->roles)) && (in_array('B', $author->roles))))

?>

Use parts and/or combinations of this code according to your needs. Pay attention to the number and placement of brackets.

In place of "TRUE" and "FALSE", of course, you may print anything your little heart desires, at any length - including all sorts of html and drupal code. For example:

<?php

{
print
"
<table>
<tr>
<td>
- The rain in Spain stays mainly in the plain!<br>
- Now, once again, where does it rain?<br>"
;

print
$node->field_details[0]['value'];

print
"
<br>
THE END
</td>
</tr>
</table>"
;
}

?>

Enjoy!




 
 

Drupal is a registered trademark of Dries Buytaert.