Advertising sustains the DA. Ads are hidden for members. Join today

Computed Field

From a user profile (or other content type) check for role(s) of the author

Last updated on
30 April 2025

I recently wanted a view display to list a number of user profile account information. Some of the user accounts have been "moderated" and others are awaiting moderation. I wanted to display a message in the view that said their moderation status. Here's how I did it:

/**
 * display a message saying whether or not the author is 'moderated'
 * 
 * For a view over Nodes that lists user profiles, I want to check for a role.
 * This loads the user profile node, and looks to find if 'moderated user'
 * is among the roles array of the profile's user. If so, they're considered
 * a 'Moderated' user, otherwise they are 'User not yet moderated.'
 */

/* configuration variables */

$registered_role = 'moderated user';
$yes_text = 'Moderated';
$no_text = 'User not yet moderated';

/**
 * Add a NID field on the user profile and fish it out using print_r($data),
 * the change the line below to $data->{your fieldname}
 */
$my_user_profile_nid = $data->node_node_data_field_my_user_profile_ref_nid;

/* test for role and assign output */
$my_user_profile = node_load($my_user_profile_nid);
$my_user = user_load($my_user_profile->uid);
$is_registered = in_array($registered_role,$my_user->roles) ? $yes_text : $no_text;
//print_r($data);
print_r($is_registered);

Help improve this page

Page status: Not set

You can: