Drupal 6 Theming Cookbook
上QQ阅读APP看书,第一时间看更新

Controlling block visibility based on node type

Thus 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.

Getting ready

We will be configuring the Recent comments block—which is provided by the Comment module—to only be visible for the story and blog node types. While the story type is enabled by default, the blog type is created only upon enabling the Blog module from the module administration page at admin/build/modules (Home | Administer | Site building | Modules).

It is assumed that both the Blog and Comment modules have been enabled, and sample nodes and associated comments have been created.

How to do it...

Block visibility can be configured from the block's configuration page as per the following steps:

  1. Navigate to admin/build/block (Home | Administer | Site building | Blocks).
  2. Look for the block which needs to be configured—Recent Comments—and click on the Configure link next to it.
  3. Navigate down to the Page specific visibility settings section.
  4. Select Show if the following PHP code returns TRUE and enter the following code in the Pages textarea below:
    <?php
    // Array of allowed types.
    $types = array('blog', 'story');
    // Check if the current page is a node.
    if ((arg(0) == 'node') && is_numeric(arg(1))) {
    $node = node_load(arg(1));
    return in_array($node->type, $types);
    }
    ?>
    
  5. Click on the Save block button to save the changes.
  6. Back on the block administration page, ensure that the block has been enabled and added to one of the current theme's available regions such as the left or right sidebar.

How it works...

Since we have chosen to control block visibility using PHP, Drupal evaluates the code within the Pages text area and only displays the block if it returns the value TRUE.

The variable $types contains the list of allowed node types. This list is populated with the internal names of each node type which can be accessed via the Content types page at admin/content/types (Home | Administer | Content | Content types).

How it works...

In the previous screenshot, the internal names of each type are listed under the column titled Type.

Since blocks are not restricted to only be visible on node pages, we need to ascertain if the page that is currently being viewed is a node page. We do this by checking the path to see if it matches the pattern node/node-id. In other words, we only consider displaying the block if the path involves the word node followed by the node ID, an integer.

Once we have ensured that we are viewing a node, we need to get Drupal to load the node using the node_load() function so that we can check if its type matches one of the types that we have allowed.

If the type of the node being viewed is in this list of allowed types, then the in_array() call returns TRUE and the block is displayed. Else, it returns FALSE which leads to the block not being displayed.

Tip

Good practice

Always add comments to code. If the code snippet has been taken from a website or a book, add a comment mentioning the URL or page number for future reference.