Display roles in profile? I'm trying to display roles in the default user profile (no profile2) with consistent formatting to the other fields being displayed.

In user-profile.tpl.php I have this:

&lt;div class="profile"&lt;?php print $attributes; ?&gt;&gt;
    &lt;?php
    // var_dump($user_profile); exit;
    $account = user_load(arg(1));
    $roles = '';
    foreach ($account-&gt;roles as $rid =&gt; $role) {
        $roles .= "&lt;li&gt;".$role."&lt;/li&gt;";
    }
    if (count($roles) &gt; 0) {
        $user_profile['field_user_roles'] = array(
            '#entity_type' =&gt; 'custom',
            '#bundle' =&gt; 'custom',
            '#theme' =&gt; 'field',
            '#field_type' =&gt; 'text',
            '#title' =&gt; 'Roles',
            '#label_display' =&gt; 'above',
            '#field_name' =&gt; 'field_user_roles',
            '#markup' =&gt; '&lt;ul&gt;' . $roles . '&lt;/ul&gt;',
            '#weight' =&gt; 5,
        );
    }

     ?&gt;
  &lt;?php print render($user_profile); ?&gt;
&lt;/div&gt;


Which is giving me this output (which is close):



The problem is that it doesn't seem to be using #markup to display the field.

I'm getting these errors which indicate I'm doing something wrong:


  Notice: Undefined index: #items in template_preprocess_field() (line 1060 of /path/to/site/modules/field/field.module).
  
  Warning: Invalid argument supplied for foreach() in template_preprocess_field() (line 1060 of /path/to/site/modules/field/field.module).
  
  Notice: Undefined index: #view_mode in zurb_foundation_preprocess_field() (line 311 of /path/to/site/sites/all/themes/zurb_foundation/template.php).


I'm also using the Foundation Drupal template (v7.5) which seems to relate to the above errors.
Updated answer

Scrolling through the Drupal.org module library, I discovered Role expose. I have never used it, but it seems to do the trick as well:


  Role Expose -module gives site administrators ability to expose user
  their own user roles. Roles are listed on user profile page. Users may
  be granted an option to view own roles or all users roles.
  
  


Original answer

Looking at the render array, I feel you misunderstood a bit (unless you have configured a field on the user object where you already put a string of all his roles, but then you would not need the logic here)...

$user_profile['field_user_roles'] = array(
  '#entity_type' =&gt; 'custom',          // Nope, it is 'user'.
  '#bundle' =&gt; 'custom',               // Nope, it is 'user'.
  '#theme' =&gt; 'field',                 // Probably it is not a field, but a custom render element containing markup.
  '#field_type' =&gt; 'text',             // Probably not since it probably wasn't a field.
  '#title' =&gt; 'Roles',
  '#label_display' =&gt; 'above',
  '#field_name' =&gt; 'field_user_roles', // Probably not since it probably wasn't a field.
  '#markup' =&gt; '&lt;ul&gt;' . $roles . '&lt;/ul&gt;',
  '#weight' =&gt; 5,
);


I guess it works better if you remove some of the data (see below).

On a small side note, logic should be avoided as much as possible within template files. So I'd recommend creating a small custom module and implementing hook_user_view_alter(). (If you are planning on supporting only one theme, you can also place it in the theme's template.php file. Use the theme name as the module name.)

function MODULENAME_user_view_alter(&amp;$build) {
  // Obtain the account that user_view() stored for us.
  $account = $build['#account'];

  // Prepare roles string.
  $roles = '';
  foreach ($account-&gt;roles as $rid =&gt; $role) {
    $roles .= "&lt;li&gt;".$role."&lt;/li&gt;";
  }

  if (count($roles) &gt; 0) {
    $build['modulename_user_roles'] = array( // Prefix with module name to prevent collisions.
      '#type' =&gt; 'item',
      '#title' =&gt; 'Roles',
      '#markup' =&gt; '&lt;ul&gt;' . $roles . '&lt;/ul&gt;',
      '#attributes' =&gt; array('class' =&gt; 'class-one class-two'),
    );
  }
}


Additionally you can implement hook_field_extra_fields() to expose the property on example.com/admin/config/people/accounts/display and choose its weight within the admin interface.

function MODULENAME_field_extra_fields() {
  $extra['user']['user'] = array(
    'display' =&gt; array(
      'modulename_user_roles' =&gt; array( // Make sure the name matches your custom property
        'label' =&gt; t('User roles'),
        'description' =&gt; t("Lists the user's roles."),
        'weight' =&gt; 5,
      ),
    )
  );

  return $extra;
}


Combined, the property should have been added to the $user_profile array and there is no more need to override the template file.

If you want to use the item_list, as proposed by Yogesh, your render array in MODULENAME_user_view_alter() should look something like this:

    $build['modulename_user_roles'] = array(
      '#title' =&gt; 'Roles',
      '#theme' =&gt; 'item_list',
      '#items' =&gt; $account-&gt;roles,
      '#type' =&gt; 'ul',
      '#attributes' =&gt; array('class' =&gt; 'class-one class-two'),
    );