Displaying a block only on the front page
This recipe details the steps involved in displaying a block only on a certain page, which in this case, is the front page. We will be displaying the welcome message block created in the previous recipe as an example.
Getting ready
The front page is a special case on most sites as it usually showcases the rest of the site. Manipulating block visibility for front page blocks is a frequent requirement and, in our case, we are going to ensure that the welcome message block is only going to be displayed on the front page and nowhere else.
How to do it...
Block visibility is controlled from the block's configuration page as follows:
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 configuration page, scroll down to the Page specific visibility settings section and select the Show on only the listed pages radio button.
Further down in the textarea titled Pages, add the word
<front>
.Click on the Save block button at the bottom of the page to save the changes.
How it works...
Whenever a block is to be displayed, Drupal checks to see if we have any visibility settings applied to it. In this case, we have Show on only the listed pages switched on. As a result, Drupal checks the Pages textarea to see which pages have been listed. The use of the <front>
keyword, which is a special indicator that represents the front page of the site, tells Drupal that unless this is the root of the site, this block should not be displayed.
This is all done before the content of the block is processed by Drupal thereby improving performance and making this method cleaner and more efficient than hiding the block using CSS or elsewhere in the theme.
There's more...
Drupal offers a number of page-matching options to further help refine when and where we display our blocks.
Multiple pages
Multiple pages can be specified in the Pages textarea. For example, if the block is to be displayed on the front page and on user pages only, the list would be the following:
<front> user/*
Drupal will now compare the path of the page against each entry in this list and decide to display the block only if there is a match.
Tip
Wildcards
The use of the asterisk wildcard in user/*
states that the match should be performed against all paths beginning with user
. This ensures that the block is displayed for all pages within every user's My account section.
Matching against URL aliases
Drupal's Path module allows users to specify aliases for nodes and system paths. While this might lead to the conundrum of which paths to use while specifying a block's page-visibility settings, the Block module's page-matching code intelligently compares against both possibilities. For example, consider the following table which specifies the internal paths and corresponding aliases for three nodes:
Internal path |
URL alias |
---|---|
node/1 |
products/foo |
node/13 |
products/bar |
node/22 |
products/baz |
If we wanted to match against all three nodes, we could specify the three node paths directly:
node/1
node/13
node/22
Or, we could specify the three aliases as follows:
products/foo
products/bar
products/baz
Alternatively, we could simply use the aliases with a wildcard:
products/*
Exclusive display
This recipe can also be similarly applied to display a block on all pages but the front page. This involves choosing the Show on every page except the listed pages option in the Page specific visibility settings section.