Restricting Content Visibility in Drupal

Today I needed to display content in a Drupal template file only for users who belong to a certain role. Specifically, I wanted to show a profile edit link (generated by a View), but only show it for those who are logged in and allowed to administer other users.

Drupal has a handy function called user_roles that is included with the User module. It will return an array of existing roles, so we just need to check it for the admin role:

<?php
if (in_array('admin', $user->roles)) {
// Put stuff here
} ?>

Where ‘admin’ is the role you are looking for. Easy-peasy.

Advertisement
Published in: on July 28, 2010 at 14:26  Leave a Comment  

The URI to TrackBack this entry is: http://mhogue.wordpress.com/2010/07/28/restricting-content-visibility-in-drupal/trackback/

RSS feed for comments on this post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.