Book Image

Drupal 6 Theming Cookbook

Book Image

Drupal 6 Theming Cookbook

Overview of this book

Themes are among the most powerful features that can be used to customize a website to fit your needs. The greatest strength of Drupal lies in its design, which, when done right, allows developers to customize every aspect of the site. Although it might sound easy to customize the look of your site, it's not a cakewalk to build custom themes that are easy to administer and maintain.Drupal 6 Theming Cookbook provides a plethora of recipes that enable Drupal template designers to make full use of Drupal's extensibility and style their site just the way they want it. It is a well-rounded guide, which will allow users looking to theme their Drupal sites to do so by taking full advantage of Drupal's theming system. It covers numerous aspects from creating custom themes to using the powerful CCK, Views, and Panels modules to create rich designs that are easy to administer and maintain.Structured as a collection of recipes to perform a wide variety of tasks, this book will guide readers through most important aspects of Drupal theming. It starts off with recipes dealing with the basics of Drupal's theme system: you will find recipes for solving all your problems with regions and blocks. It then moves on to advanced topics such as creating a custom theme and using it to modify the layout and style of the content that is output on a page. A substantial number of recipes are dedicated to Drupal's template system, which will provide you with a solid foundation in order to override the output of Drupal and contributed modules. Furthermore, as the combination of modules such as CCK, Views, and Panels is so widely prevalent, chapters have been dedicated for each of these modules. With this book, you'll learn to get the most out of Drupal's templating system and its modules to create rich designs for your site.
Table of Contents (18 chapters)
Drupal 6 Theming Cookbook
Credits
About the Author
About the Reviewers
Preface
Index

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

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.