Controlling block visibility based on user role
This recipe details the steps involved in toggling block visibility based on which type of user is viewing the page. For example, a block displaying advertisements might only need to be visible for anonymous users and not for authenticated users.
Getting ready
For this recipe, we will be configuring the welcome message block which we created in an earlier recipe in this chapter, to only be visible to authenticated users.
How to do it...
Controlling block visibility is handled from the block administration pages as outlined in the following:
Navigate to
admin/build/block
(Home | Administer | Site building | Blocks).Locate the block which needs to be configured—the
Welcome message
block—and click on the Configure link next to it.On the configure screen, scroll down to Role specific visibility settings.
Check the box corresponding to the authenticated user role which should have access to the block.
Click on the Save block button at the bottom of the page to save the changes.
How it works...
Drupal maintains a table named block_roles
which keeps track of role-specific settings for all blocks. Changes made to role settings on the block configuration page are stored in this table. When an anonymous user now visits the site, Drupal will not display the Welcome message
block.