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.
Is there some good reason this hasn't already been implemented? If i worked on a patch to block.module for this, is there any hope it'd be committed to the core drupal distribution? My site is all based on 4.6, and i know a new feature like this isn't going to go into the main 4.6. however, if i got this working in a 4.7 test site, is there any chance it could be added before 4.7 is made "stable"? I don't want to spend the time working on this if folks don't think this is a good idea. On the other hand, it seems like it'd be a pretty major improvement to the usability of drupal.
Thanks,
-Derek
Comments
Discussion
See http://drupal.org/node/18018 for discussion & patches
--
Tips for posting to the forums.
When your problem is solved, please post a follow-up to the thread you started.
oh well, here's a 4.6 version of this feature...
i just patched my own 4.6 site's block.module to do this, before i saw the other thread. seems like i did a very similar thing to some of what was discussed in node/18018. ;) it looks like this new feature will soon make it into 4.7, which is great. alas for the duplication of effort on my part, but at least it was a useful learning experience for me. thanks for the link, Heine.
if anyone out there is running a 4.6 site and wants this functionality, i uploaded a patch at http://drupal.org/node/49154. feel free to use it on 4.6 sites, since this feature will only be officially supported in 4.7 and beyond.
enjoy,
-derek
___________________
3281d Consulting