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
Comment #1
BassPlaya CreditAttribution: BassPlaya commentedAnyone wanna share some code with me? Cheers!
Comment #2
BassPlaya CreditAttribution: BassPlaya commentedEverybody went skiing dude!
Comment #3
rsgracey CreditAttribution: rsgracey commentedI 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.
Comment #4
BassPlaya CreditAttribution: BassPlaya commentedThat'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.
Comment #5
BassPlaya CreditAttribution: BassPlaya commentedis there really no one who can or is willing to shed some light on this? swentel?
Comment #6
swentel CreditAttribution: swentel commentedAre 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
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 :)
Comment #7
BassPlaya CreditAttribution: BassPlaya commentedThanx 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. ;-)
Comment #8
BassPlaya CreditAttribution: BassPlaya commented@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
Comment #9
BassPlaya CreditAttribution: BassPlaya commentedI'm sorry but I still can't figure this out. Please give me an example code. Thanx.
Comment #10
merauluka CreditAttribution: merauluka commentedFor 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:
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.
Comment #11
pfrenssenHehe did someone call me? :D
First of all a warning: personally I would never use code fields. Code fields have many pitfalls:
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":
This will list up the user's roles, capitalized and comma separated:
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:
When creating these code fields, make sure to select the "User" entity, and choose the "Display Suite code" text format.
Comment #12
pfrenssenMarking fixed. If more info is needed then please reopen.
Comment #14
jjmackow CreditAttribution: jjmackow commentedIf 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);
?>