Searching on drupal.org, i've found literally dozens of posts asking about how to display or hide blocks based on the role of the user viewing a page. Currently, the only ways to do this are to either:
- Define a new custom php block that checks the role
- Write a whole new module, just to implement hook_block()
This seems like such a basic, fundamental feature for blocks, something many drupal users want to be able to do. Sadly, neither of these options are very easy, particularly for people not familiar with php or writing code in general. Clearly, many hours of effort have been spent by people trying to get this working, generating traffic in the support forums, etc. Why can't this feature just be built into the block module itself?
For example, we could add another field to the {blocks} table called something like "roles". This would hold a list of role ids for which the given block should be displayed. for any given block being published in block_list(), when we query the {blocks} table, we also grab this field. when checking to see if the given block should be displayed, we add another check to see if any of the roles in the global $user object match a role from this field. if there are no values in the "roles" field at all, that would mean to display the block (assuming the existing logic said the block should be displayed). Then, in the admin/block/configure page to control the "custom visibility settings" for any given block, we could add another form group called "Restrict block to these roles", with a set of checkboxes for each role defined on the system. Just like the existing "Restrict block to specific content types:" set of checkboxes, if none of them are checked, we'd leave the "roles" field blank in the {blocks} table, which would mean it should be displayed for all roles.