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

Anyone wanna share some code with me? Cheers!

Everybody went skiing dude!

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.

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.

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

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

<?php
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 :)

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. ;-)

@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

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

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.

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":

<?php
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:

<?php
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:

<?php
$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.

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.