Next Tutorial

Drupal 7 Tutorial Part 16: Drupal 7 Reports & Logs Explained

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

Submitted by anilsagar on Sat, 05/14/2011 - 13:50

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 by Clicking on Structure » Blocks » configure link next to the block you just created to access block configuration page and now let's 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

7 comments

You've instructed the reader

Submitted by alok (not verified) on Wed, 05/09/2012 - 20:17.

You've instructed the reader to remove pages, content type settings before trying role based settings. Please give a similar instruction before users setting also. I forgot to remove role based settings before trying the users settings. As a result, the 'Personalize Blocks' option did not appear in my user account, and I was left wondering what was wrong.

About block creation using code

Submitted by ishwardev (not verified) on Fri, 03/23/2012 - 14:48.

Please update new tutorial for creating block using code block, I am very beginner so please help me as it is my present requirement.

I see that you just mixed up your pictures...

Submitted by Matthew (not verified) on Mon, 12/19/2011 - 13:09.

your picture should be changed. You say one thing yet your picture shows something else. Very confusing.

I guess you could have simply wrote, " Home » Administration » Structure » Blocks" & not mix up the picture.

Can't follow your direction.

Submitted by Matthew (not verified) on Mon, 12/19/2011 - 12:18.

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.

I cannot follow this part. When I click structure -> content type, I see a completely different page. I cannot get to that third picture (from the bottom.) What am I doing wrong?

Thank you :-)

Submitted by anilsagar on Mon, 12/19/2011 - 17:39.

Dear Matthew,

Thank you for your suggestion. I made changes to the text like you mentioned in the comment.

Now people can access block configuration page easily i hope.

Once again thank you very much :-)

Cheers,
Anil Sagar

display some content into a block

Submitted by gerard (not verified) on Sat, 12/03/2011 - 17:33.

Tutorial is great ... howevever i could not solve my problem.

I want to display some text in a "side bar" block.
So i created a new block and placed it in the region / sidebar.
I also created a new content-type (may be was not necessary) and a new content (the text i want to display).
Now i can't get this damn content to show in the block.
All i get is the body of the block which indeed displays in the side bar.
I don't need/want to use any menu for that.

Is there a way to do it ?

Thanks.

Hi Gerard

Submitted by anilsagar on Mon, 12/05/2011 - 17:34.

Use Drupal views module to dynamically pull content from nodes of a content type in block format.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.
©2010 AnilSagar. All rights reserved. Drupal theme by Kiwi Themes.