I'm NO php-er but I would like to know how I can display fields of a user depending on their role (meaning conditionally in a php statement in the code field).
I've figured out how to check for the role but the BASIC part of simply displaying a field and/or it's label with php and probably not with tokens conditionally beats me and is nowhere explained in documentation for as far there is a documentation on this topic nor can I find an affirmative mention of the code in the issue queue.

I've used

<?php
$uid = arg(1);
$user = user_load($uid);
$roles = $user->roles;
if (in_array('administrator', array_values($roles))) {
print ('This works!');
}
?>

but how do I display
a) for example the name of the role of the user's profile a visitor is seeing
b) any kind of field that was created here: admin/config/people/accounts/fields

Help would be greatly appreciated!

Comments

BassPlaya’s picture

Anyone wanna share some code with me? Cheers!

BassPlaya’s picture

Everybody went skiing dude!

rsgracey’s picture

I can't get it to work, either. Whether tokens or not, whether filtered, full, or plain, I just get the exact code displayed, rather than what it's supposed to do.

BassPlaya’s picture

That's because you need to set the text format to Display Suite Code. Then you'll be able to show everything like I stated above but I still don't know how to display the things I need in my original post.

BassPlaya’s picture

is there really no one who can or is willing to shed some light on this? swentel?

swentel’s picture

Are we looking at a user profile page here ? I assume so, since you're using arg(1).
What's also available in this code is the user object as '$entity'. I'd suggest using devel and adding a

dsm($entity);

to the code so you can see which fields are on the user object. Than you should be able to print out values, either directly printing them out, or using field_get_items() (haven't tested the latest).

Let's hope maybe pfrenssen chimes in here, he's on a roll regarding giving code examples :)

BassPlaya’s picture

Thanx Swentel! I'll try that and see where it leads me. I'll come back if I don't understand and I'll try to follow pfrenssen. ;-)

BassPlaya’s picture

@swentel: not a clue. Could you give me an example of how to show the user picture for example with a code field? I might be able to deduct the logic from your code. Thanks

BassPlaya’s picture

I'm sorry but I still can't figure this out. Please give me an example code. Thanx.

merauluka’s picture

For the role of a user, couldn't you just do a walk through the array of roles and print the results? I suppose it depends on whether your users are going to have a ton of roles or not. But you could easily do that with:

<?php
  $uid = arg(1);
  $user = user_load($uid);
  $roles = $user->roles;
  if (in_array('administrator', array_values($roles))) {
    print ('<h3>User Roles</h3><ul>'); 
    foreach ($user->roles as $role) {
      print "<li>".ucwords($role)."</li>";
    }
    print "</ul>";
  }
?>

Bear in mind that I have not tested this code...but it looks like it would do the trick for the first portion of your request.

pfrenssen’s picture

Hehe did someone call me? :D

First of all a warning: personally I would never use code fields. Code fields have many pitfalls:

  • A mistake in the code could cause a WSOD which is only solvable by editing the database directly.
  • The code cannot be version controlled or grepped easily.
  • It's easy to forget that code fields exist, which could cause confusion for future maintainers (or your future self!)
  • Code fields can cause dramatic security issues if they would get exposed to the wrong users.

So I do not endorse the following example, instead I would put this code in a custom module. But I can understand that not everybody is equally at home with creating modules, so the code fields have their merit. Just be very careful and document what you are doing.

The following example outputs a badge if the user has the user role "premium member":

if (in_array('premium member', $entity->roles)) {
  return '<img src="/sites/all/themes/mytheme/images/premium_badge.png" alt="Premium member">';
}

This will list up the user's roles, capitalized and comma separated:

return ucwords(implode($entity->roles, ', '));

This will output the content of the "Occupation" text field (machine name: field_user_occupation) which has been added at admin/config/people/accounts/fields. When multiple occupations are given, these are comma separated:

$occupations = array();
foreach (field_get_items('user', $entity, 'field_user_occupation') as $occupation) {
  $occupations[] = $occupation['safe_value'];
}
return implode($occupations, ', ');

When creating these code fields, make sure to select the "User" entity, and choose the "Display Suite code" text format.

pfrenssen’s picture

Status: Active » Fixed

Marking fixed. If more info is needed then please reopen.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

jjmackow’s picture

Issue summary: View changes

If you are doing this inside of a node, the NID is derived from the first Argument; arg(1);
load the node with nod_load;
in this example, taxonomy IDs are stored inside of a taxonomy entity reference field called field_person_category,
you can find all of the field values within the array using the print_r(); function;
so for instance, you could insert

print_r($node);

after line 2,

$node = node_load($nid);

<?php
  $nid = arg(1);
  $node = node_load($nid);
  $tids = $node->field_person_category[und]; 
  foreach ($tids as $tid){
    $term = $tid[tid];
    //if the $term is either 22 (faculty) or 112 (Staff) then print either label respectively;
    if( $term == 22){
      print "<h2>Faculty</h2>";
    } elseif ($term==112 ) {
     print "<h2>Staff</h2>";

?>