0

I need to show a block to users who have multiple roles. For example: accountant and employee. I do not want to use permissions. I need a simple solution.

is there anyone who can direct me?

3 Answers 3

2

You could use hook_block_view_alter() in your theme's template.php

function MYTHEME_block_view_alter(&$data, $block) {
  global $user;

  $roles = array('accountant', 'employee');
  $required_roles = array_diff($roles, $user->roles);

  if ($block->delta == 'YOUR-BLOCK-DELTA' && !empty($required_roles)) {
    $data['content'] = $data['subject'] = NULL;
  }
}

The array_diff() here will check that the roles the user needs to have appear in the user's roles array. If either accountant or employee do not appear in $user->roles it will set the block's content/subject to NULL and Drupal will not display it.

1

You can use Context module which has possibility to display content and it supports AND/OR operations for specific conditions (such as role selection).

2
  • there isn't any condition base on username. you can define it under roles. nothing changes!!
    – shekoufeh
    Commented Feb 14, 2015 at 6:49
  • @shekoufeh Why there should be a condition per username? It's not Drupal-way. For that reason there are roles. You apply specific role to the username to which you want to show a block.
    – kenorb
    Commented Feb 14, 2015 at 10:52
0

You can show a block for specific roles on the configurationpage of the block. Navigate to admin/structure/block and click "configure" next to the block you want to set the restrections. Then scroll down to "visibility settings" and select for which roles the block must be visible: enter image description here

3
  • 1
    I guess if we do like this, the individual roles (who have only accountant or employee) can access the block. But OP don't want that. Commented Feb 12, 2015 at 7:48
  • 1
    I do not think this solution works for both roles(and condition only)
    – DRUPWAY
    Commented Feb 12, 2015 at 7:52
  • in this way accountants or employees can see the block to! I need to display it just to people who have both of rolls employee AND accountant!
    – shekoufeh
    Commented Feb 12, 2015 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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