18.10.2016 Views

Drupal 7 Module Development

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Drupal</strong> Permissions and Security<br />

If the current user passes this access check, we must then fetch the information we<br />

want. This information is not about the $user but about the $account being viewed.<br />

So we add the logic:<br />

/**<br />

* Implement hook_user_view().<br />

*/<br />

function example_user_view($account, $build_mode) {<br />

if (!user_access('view user roles')) {<br />

return;<br />

}<br />

// Get the user's roles.<br />

$list = $account->roles;<br />

if (!empty($list)) {<br />

// Prepare the information for theming.<br />

$variables = array(<br />

'items' => $list,<br />

);<br />

$content = theme('item_list', $variables);<br />

// Attach the content to the user page according to the API.<br />

$account->content['summary']['output'] = array(<br />

'#type' => 'user_profile_item',<br />

'#title' => t('User roles'),<br />

'#markup' => $content,<br />

'#attributes' => array('class' => array('content-creation')),<br />

);<br />

}<br />

}<br />

When implemented, our code produces the following result on a user page:<br />

[ 214 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!