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

Adding a custom block to the theme


This recipe details the steps involved in adding a block with custom content to the theme. Drupal blocks can either be declared using a module or, as we are doing here, added manually via the block administration interface.

Getting ready

For this recipe, we will be adding a simple welcome message in a custom block within a predetermined region. As with standard blocks, position matters!

How to do it...

The following procedure outlines the steps required to add a custom block to a theme:

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

  2. 2. If more than one theme is enabled, select the theme that we are adding our block to by clicking on its tab.

  3. 3. Click on the Add block link at the top of the page.

  4. 4. In the ensuing page, type a welcome message in the Block description textfield.

    Note

    This description field comes in handy on the block administration page when trying to differentiate between blocks with identical titles, or as is frequently the case, no titles.

  5. 5. Next, if the block requires a title to be displayed above its content, add one via the Block title textfield. In this case, we do not need one as we are just looking to display a welcome message.

  6. 6. As displayed in the following screenshot, enter the welcome text into the Block body textarea: Welcome to Mysite. Enjoy your stay!.

    Note

    Similar to most other textareas in Drupal, a linked Input format should be available to filter the content appropriately. This allows for great flexibility when adding content.

  7. 7. The Region settings fieldset lists all currently enabled themes. Optionally, choose the region where this block is to be displayed for each of them (or None if it is not to be displayed at all).

  8. 8. Finally, click on Save block to create the block.

How it works...

Just as with standard blocks, Drupal maintains a table named block_custom which tracks all custom blocks including their content and input format. Once a custom block is enabled, it is added to the block table and tracked as if it was a standard block.

When created, a custom block appears in the block list and can be treated just like any other block. It can be dragged around different regions, have its visibility settings controlled, and so on. The following screenshot displays our newly created welcome block as part of the Garland theme:

Note

An easy way to identify custom blocks on the block management page is by their tell-tale delete links. Only custom blocks feature a delete option.

There’s more...

Custom blocks are useful for more than simply embedding text strings.

Doing more with custom blocks

Custom blocks can be very handy to not only add visible content, but also to execute short code snippets on specific pages provided the appropriate input format has been selected. For example, we could embed some custom JavaScript required only for a few specific page nodes, by adding it to a custom block—equipped with a suitable input format—which is set to be displayed only with the aforementioned page nodes.

That said, if a more optimal solution is available—such as using a module to hold our code—then it should be pursued instead of inserting code into blocks and thereby into the database.

See also

Now that we have seen how to add and manage blocks, we can proceed to control it further by playing with its visibility configuration. The final three recipes of this chapter outline the steps required for Displaying a block only on the front page, Controlling block visibility based on user role, and Controlling block visibility based on node type.