Book Image

Drupal 7 Theming Cookbook

By : Karthik Kumar
Book Image

Drupal 7 Theming Cookbook

By: Karthik Kumar

Overview of this book

<p>The greatest strength of Drupal lies in its design which, when employed correctly, allows developers to literally handcraft every aspect of a site, so that it looks and performs exactly how they want it to. While it is reasonably straightforward to download a Drupal theme and install it, doing anything beyond that is not. Using custom themes requires familiarity and experience with Drupal's theming system, especially if you want to easily administer and maintain your themes.</p> <p>Drupal 7 Theming Cookbook provides a plethora of recipes that enable Drupal template designers to make full use of its extensibility and style their site just the way they want it. It is a well-rounded guide which will allow users to take full advantage of Drupal's theming system.</p> <p>This cookbook starts with recipes which address the basics of Drupal's theme system, including 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 content. With the introduction of the Field API and the growing importance of Views and Panels in Drupal 7, chapters have been dedicated to each feature. You will also learn many techniques for dealing with Drupal&rsquo;s templating system, which will allow you create themes which surpass even the existing Drupal and contributed modules.</p>
Table of Contents (18 chapters)
Drupal 7 Theming Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface

Controlling block visibility based on node type


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.

Getting ready

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.

How to do it...

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

  1. 1. Navigate to admin/structure/block [Home | Administration | Structure | Blocks].

  2. 2. Look for the block that needs to be configured, Recent comments, and click on its Configure link.

  3. 3. On the configure screen, scroll down to the Visibility settings section and select the Content types tab.

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

How it works...

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.

See also

Earlier recipes in this chapter, namely Controlling block visibility based on user role and Displaying a block only on the front page, also concern controlling block visibility.