Drupal 7 Tutorial Part 6: Understanding Drupal 7 Block Visibility Settings

posted Jun 23, 2011, 12:56 AM by admin@ revitviet.vn

Hi Drupalers,

Last tutorial we saw how to configure block region settings. In this tutorial we are going to learn how to use drupal block visibility settings.

Before going to see how to use drupal block visibility settings let me tell you what is the use of these settings. Basically, Visibility settings as the name tells used to hide or display our block in different pages based on user roles and pages etc. Lets get into action by following below steps.

  • Login as admin and hover on the block you created you can see a link to configure block, when you click on wheel like icon at top right hand side. Using this link directly we can go to block configuration settings. This link is only visible to administrator or user with proper permissions to manage blocks.

 Visibility Settings:

  • Once you are in block configuration page scroll down to bottom to see visibility settings area just like shown in below image.

  • As you can see there are four different settings in visibility settings. Namely,
    • Pages
    • Content Types
    • Roles
    • Users

             Let's check it out one by one.

Pages:

  • This setting is used to either display the block other than specifically listed pages or display the block only in specifically listed pages in the text area as shown in above image.
  • Let's check the first setting in this by selecting All pages except those listed.
  • Enter the below pages separated by new line in the text area below check boxes.
    • node/1
    • admin/structure/block/manage/block/1/configure
  • Now click on Save Block button to save settings.
  • To see the changes, goto node/1 page and admin/structure/block/manage/block/1/configure page by entering the url like http://localhost/d7/node/1 or http://localhost/d7/admin/structure/block/manage/block/1/configure as you can see the block is not displayed in both the pages, because in block settings we specifies show the block other than below listed pages.
  • If you go to home page you can see the block in left side bar. Because home page and any other page falls under other than mentioned pages in the text area.
  • The second check box in pages i.e Only this listed pages is exactly opposite of above setting. Means, block is displayed only in the listed pages in the text area.

Block Content Type Settings:

  • Before playing with setting we need to create some content of different types using add content option link below admin menu.
  • Click on Add Content link to see content type selection page just like below screen.

 

  • Click on article and create a article with title Article One , save the content.
  • Again repeat above steps to create a basic page with title "Page Sample", save the content.
  • Now come back to our block configuration page to check out how Content Types setting works.
  • When you Click on Content Types setting tab, you will see screen something like below.

  • When no content type is selected block is displayed in all types of node pages. To understand what is a node please check my earlier tutorials.
  • Let's select Article type and click on Save Block. Now our block will appear only in node type article pages but not in basic page nodes.
  • To verify please goto "Article One" page. To go to this page Click on Find content link in admin menu bar, Just below black menu and click on Article One node on the list.
  • You can see the block in the page because the node "Article one" is of node type/content type Article so it appears.
  • If you go to page sample node that you created block wouldn't appear because it is of content type Basic page, which you didn't select in content type settings in the block.

Roles Based Visibility Setting:

  • We can configure block to display only to certain users classified by roles. Let me tell clarify you what is a role in drupal before jumping into this settings.
  • Role is nothing but classification of users into different categories with various levels of permission. By default in Drupal 7 you can find three types of roles. Anonymous (User who is not logged in), Authenticated (Logged in user), Administrator (User who has access to admin related pages like add blog, configure block etc.)
  • You can create new roles like moderator and assign some permissions to that role. I will discuss roles in detail in my future tutorial.
  • So, basically you can configure block only visible to particular types of roles. User who has that role can only see the block.
  • Let's see in action by configuring this setting. Before using this setting reset all other visibility settings we discussed above. Remove page, content type settings.
  • Now click on roles and select only anonymous user and save the block.
  • After this setting logged in user cannot see the block we created. Only guest / anonymous user can see our block in home page.

Users Setting:

  • This setting allow individual users to hide or display block base on their choice.
  • When you select this setting you can see something like below.

  • By Default this setting is configured to Not Customizable.
  • Let's select Customizable, Visible by default setting  which allows individual users hide a block or display a block option. By default block is showed when you use this setting.
  • Select this and click on save button.
  • Now let's go to user account to hide or display contact us block. Navigate to /user page to visit user account page.
  • You will see something like below screen.

  • Now click on edit tab next to view tab to configure our block to display or hide.
  • Scroll down to see the block settings that is individual to the user. Using this option individual user can hide or display block.

 

  • As shown above, if you uncheck the contact us Block checkbox and save the settings block is no more visible to that particular user. Other users can able to access the page.
  • Similarly like this setting if you choose, Customizable, hidden by default setting then by default block is hidden but user can enable the block just by navigating to edit page of account, just as explained above.

 

Before finishing this lengthy article i just want to say if multiple settings are chosen for block like content types, roles then AND condition is applied to display the block. For example i selected article in content type setting, and Anonymous role in roles setting then block is visible for anonymous users in nodes of article type.

Hope you enjoyed this lengthy article.

 

Cheers,

Anil Sagar

Comments