Display Content for a Specific Role - Drupal
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!
can you please tell me where
can you please tell me where do i have to insert this code
thnx in advance
can you please tell me where
can you please tell me where do i have to insert this code
thnx in advance
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.
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
This seems an excellent
This seems an excellent features to increase overall user experience and by the way to save some of our resources. An interesting features, will give it a try. Thanks for Share.
I read your blog all the time
I read your blog all the time and I just thought I’d say keep up the terrific work!
This is the best example of
This is the best example of advancement in technology and development. I too had a similar requirement from one of my client but was unable to find a way, here it seems now I can accomplish the same task.
i found this tutorial very
i found this tutorial very informative and i like the way you described it :)
Thanks for this useful
Thanks for this useful content for Drupal. I helped me a lot in my work.