官术网_书友最值得收藏!

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:

  1. Navigate to admin/build/block (Home | Administer | Site building | Blocks).
  2. Locate the block which needs to be configured—the Welcome message block—and click on the Configure link next to it.
  3. On the configuration page, scroll down to the Page specific visibility settings section and select the Show on only the listed pages radio button.
    How to do it...
  4. Further down in the textarea titled Pages, add the word <front>.
  5. 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:

            

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.

Internal path

URL alias

node/1

products/foo

node/13

products/bar

node/22

products/baz

主站蜘蛛池模板: 建湖县| 岢岚县| 盘锦市| 突泉县| 古蔺县| 樟树市| 五家渠市| 宣汉县| 肃南| 江北区| 全南县| 高雄县| 衢州市| 淄博市| 平南县| 叶城县| 尤溪县| 临潭县| 山西省| 绵阳市| 商洛市| 广灵县| 时尚| 望都县| 公主岭市| 滨海县| 西丰县| 河津市| 通辽市| 岱山县| 工布江达县| 星子县| 西峡县| 莒南县| 南通市| 乌拉特中旗| 广昌县| 凌源市| 闵行区| 巴里| 温州市|