Community Documentation

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

Last updated July 19, 2011. Created by baisong on July 19, 2011.
Log in to edit this page.

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:

<?php
/**
* 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);
?>

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 6.x
Audience
Developers and coders, Site administrators

Site Building Guide

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here