So far in this chapter, we have looked at controlling block visibility based on the path of the page and the role of the user. In this recipe, we will look to configure a block to be displayed based on the node type of the content on the page.
We will be configuring the Recent comments block which is provided by the Comment module to only be visible for two particular node types, story and blog. The blog type is automatically created upon enabling the Blog module via the module administration page at admin/build/modules
[Home | Administration | Site building | Modules]. The story type and another example type named page, however, need to be created manually via the Add content type page at admin/structure/types/add
[Home | Administration | Structure | Content types].
It is assumed that both the Blog and Comment modules have been enabled and that the story and page node types have been created. It is also recommended that sample nodes and associated comments be created for all node types to reliably test our recipe.
Block visibility can be configured from the block’s configuration page as per the following steps:
1. Navigate to
admin/structure/block
[Home | Administration | Structure | Blocks].2. Look for the block that needs to be configured, Recent comments, and click on its Configure link.
3. On the configure screen, scroll down to the Visibility settings section and select the Content types tab.
4. In the Content types tab, check the checkboxes that correspond to the Blog entry and Story types as shown in the following screenshot:
5. Finally, click on Save block to save our changes.
To test if our changes have taken effect, visit pages representing each of the three node types: blog, page, and story, and verify that the Recent comments block is being displayed only for the two configured in this recipe and not the rest.
Drupal maintains a table named block_node_type
which keeps track of type-specific settings for all blocks. When a block is to be displayed, Drupal looks up this table if node type-specific conditions are in effect. If they are, then Drupal compares the type of the node being displayed against the types loaded from the block_node_type
table and displays the block only if there is a match.
In this case, the block will only be displayed if we are viewing a blog or story node.