I have a simple Drupal 7 page with 3 buttons. Page is only accessible by logged-in users.

I have 3 roles - role 1, role 2 & role 3

What I would like to do is to show only button 1 when a user with role 1 logged in, hide the other 2. How can I achieve this, please?


To hide and show buttons by role you can use the $GLOBALS to get the user and check on user role, something like.

 <?php if (in_array('role 1', $GLOBALS["user"]->roles)): ?>
    <button value="button 1">button 1</button>
  <?php else: ?>
    <button value="button 2">button 2</button>
    <button value="button 3">button 3</button>
  <?php endif; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.