Display Content for a Specific Role - Drupal

Tags:

Sometimes, is can be a requirement to only allowed a specific group of users to view some content on a page. I'm not talking about showing or hiding the entire page to a specific role.

What I am talking about is that an authenticated user sees some different or additional content than an anonymous user will see on the same page.

I recently had a requirement to do just this on a community website that I am building. I needed to show a discount Coupon Code to registered users but not to anonymous users.

This is easily achieved by changing the content type to PHP and inserting the following snippet of code.

<?php
global $user;
$approved_roles = array('authenticated user', 'super user');

if (
is_array($user->roles)) {
  if (
count(array_intersect($user->roles, $approved_roles)) > 0) {
    print
'<p>Use Coupon Code: <strong>My Code</strong></p>When <a href="http://www.example.com">Ordering Products</a>';
  }

  else {
    print
'<p><a href="/user/login?destination=current-page-url">Login</a> | <a href="/user/register">Register</a><br /> To get Coupon Code.</p>';

}}
?>

Just a few things to note about the about PHP snippet.

  • This allows users in the "super user" and "authenticated user" groups to see the coupon code.
  • Anonymous users are prompted to Register or login to see the Coupon Code.
  • The "current-page-url" should be replaced with the url of the current page. This will allow a registered user to redirect straight back to the same page after logging in.

Thanks for the snippet!

This turned out to be handy little piece of code that works flawlessly on my drupal site . Thanks bunches!

Submitted by Bob Butterworth (not verified) on Sat, 2009-02-07 08:33.

can you please tell me where

can you please tell me where do i have to insert this code

thnx in advance

Submitted by Anonymous (not verified) on Thu, 2009-03-19 23:47.

can you please tell me where

can you please tell me where do i have to insert this code

thnx in advance

Submitted by Anonymous (not verified) on Thu, 2009-03-19 23:48.

You can insert it wherever

You can insert it wherever you want, on all pages.
Just be sure to replace
/user/login?destination=current-page-url
by the URL of the page.

Submitted by Anonymous (not verified) on Sat, 2009-03-21 01:43.

Thank you very much

I just want to let you know that you really helped me with some issues for my current project!!!!! Thank you very much

Submitted by Tobias (not verified) on Sat, 2009-06-13 01:35.

i found this tutorial very

i found this tutorial very informative and i like the way you described it :)

Submitted by stunning boy (not verified) on Wed, 2011-06-22 08:14.

Thanks for this useful

Thanks for this useful content for Drupal. I helped me a lot in my work.

Submitted by enterprise app integration (not verified) on Sun, 2011-11-06 13:14.

Well, such codes are of use

Well, such codes are of use to those enthusiast developers who always try to accomplish most complex task as an ease. I will definitely share this code with my fellow friends.

Website Development

Submitted by Brain (not verified) on Mon, 2012-03-05 20:28.

This is concise but very

This is concise but very useful tutorial.
UFC Forum

Submitted by jeffsmith (not verified) on Wed, 2012-04-18 19:45.