Views- Show a table of users with each column indicating whether or not a user has a role I am using Views Data Export to get data on my Drupal users into Excel.  My site has a large number of roles (Roles A - Z), and some of these are important for user statistics.

The view I use for data export is a table of fields.  I added Roles as follows:



This gives me something like RoleA, RoleC, RoleD, RoleI for each user.  This sort of data is difficult to manipulate in Excel.

What I would like to do instead is to have a column in the table for each important role that has 1 if the user has the role, or 0 if the user does not have the role.

In other words, my table looks like this now:



But I want it to look like this in Views:
Here's a solution using a simple module and a custom field template.

Overview

The view (obviously of the User Table) should only contain one field: User: Name. All of the other Role columns are generated by implementing HOOK_views_pre_view() with a loop for each defined role. The module also defines a custom function that compares the original output (comma-separated list of roles) and returns true if the list contains the current column's role.

The custom views field template simply calls the custom function for each column that corresponds to a role.

This solution will automatically scale if you add/delete roles and adjust if you rename/reorder roles etc.

Module File

You'll obviously need an info file too. I chose to name the module views_urt (User Role Table) - but you can name it whatever you want or include these functions in an existing custom module if you choose.

&lt;?php
/**
 * Implements HOOK_views_pre_view().
 *
 * Adds a field to the User Roles view for each role defined.
 */
function views_urt_views_pre_view(&amp;$view) {
  if ($view-&gt;name == 'user_roles') {
    $roles = views_urt_roles();
    $count = count($roles);
    for($i=0;$i&lt;$count;$i++) {
      $view-&gt;display['default']-&gt;handler-&gt;options['fields']['rid_' . $i]['id'] = 'rid_' . $i;
      $view-&gt;display['default']-&gt;handler-&gt;options['fields']['rid_' . $i]['table'] = 'users_roles';
      $view-&gt;display['default']-&gt;handler-&gt;options['fields']['rid_' . $i]['field'] = 'rid';
      $view-&gt;display['default']-&gt;handler-&gt;options['fields']['rid_' . $i]['label'] = $roles[$i];

    }
  }
}

/**
 * Determines if a comma-seperated list of roles contains the role of the
 * current column.
 *
 * @param $pos integer
 *   The column corresponding to the $field-&gt;position value in the view.
 * @param $user_roles string
 *   Comma seperated list of Roles (haystack)
 * @return bool
 */
function views_urt_has_role($pos, $user_roles) {
  $pos = ($pos -1);
  $user_roles = explode(',', $user_roles);
  $defined_roles = views_urt_roles();
  foreach ($user_roles as $user_role) {
    if (trim($user_role) == $defined_roles[$pos]) {
      return TRUE;
    }
  }
  return FALSE;
}

/**
 * Helper function that returns an array of roles exluding "Anonymous" and
 * "Authenticated" user.
 *
 * @return array
 *   An array of roles.
 */
function views_urt_roles() {
  $roles = user_roles();
  array_shift($roles);
  array_shift($roles);
  return $roles;
}


Views Field Template File

This template should be picked up by all of the fields in this view for all of the displays. So, since my view is named user_roles, this template is named views-view-field--user-roles.tpl.php.

&lt;?php
if ($field-&gt;field_alias == 'users_name') {
  print $output;
}
else {
  // We're assuming that this view only has one field (User: Name). All other
  // fields are generated by the HOOK_views_pre_view() function in the
  // `views_urt.module` and those should be passed through the
  // `views_urt_has_role` function which determines if the $output
  // (comma-seperated roles per user) contains the column role.
  print views_urt_has_role($field-&gt;position, $output);
}
?&gt;


Sample Output