Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Drupal 6 Theming Cookbook
Drupal 6 Theming Cookbook

Drupal 6 Theming Cookbook: Over 100 clear step-by-step recipes to create powerful, great-looking Drupal themes

eBook
$9.99 $28.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Drupal 6 Theming Cookbook

Chapter 2. Beyond the Basics

We will be covering the following recipes in this chapter:

  • Understanding the anatomy of a theme

  • Creating a sub-theme based on a core theme

  • Overriding base theme elements in a sub-theme

  • Changing the screenshot image of a theme

  • Including a CSS file in a theme

  • Enabling CSS optimization

  • Creating the mysite module to hold our tweaks

  • Adding a CSS file from a module

  • Displaying a different theme for each day of the week

  • Creating a fresh look using the color module

Introduction


One of the more prevalent adages with respect to Drupal development and theming is:

Do not hack core!

Modules, themes, and other files which come with a stock Drupal installation should never be edited directly. In other words, we really should not need to modify anything outside the sites folder which is designed to contain all our changes and customizations. The reasoning behind this is that most, if not all, aspects of core are accessible and modifiable through a clean and non-invasive process using Drupal's APIs. Therefore, hacking core modules and themes to get things done is almost always unnecessary and ill-advised.

Another reason why directly editing core modules and themes, or for that matter, even contributed modules and themes, is that whenever an upgrade of Drupal or said modules and themes takes place, we will very likely be overwriting the changes we have made, or at the very least, make the upgrade a trying exercise.

With respect to themes, let us take the example...

Understanding the anatomy of a theme


Drupal themes can consist of a multitude of files each with its own purpose, format, and syntax. This recipe will introduce each of these types with an explanation of what they do.

Getting ready

It will be useful to navigate to the Garland folder at themes/garland to browse and view the files inside a typical, fully featured theme. Garland also uses the PHPTemplate theming engine which is the most commonly used and recommended engine across Drupal's core and contributed themes.

How to do it...

The following table outlines the types of files typically found inside a theme's folder and the naming conventions to be followed for some of them.

Type

Mandatory?

Description

mytheme.info

Yes

Configuration file which provides information to Drupal about a theme named mytheme.

page.tpl.php

Yes

A template file which determines the layout of all Drupal pages.

node.tpl.php

No

A template file which determines the layout of a node inside a Drupal page.

block...

Creating a sub-theme based on a core theme


This recipe details the steps involved in creating a sub-theme of an existing core theme.

Getting ready

Create a folder named mytheme inside sites/all/themes. This name is usually also the name of the new theme and it is best to keep things uncomplicated by not using spaces and special characters. While mytheme is suitable for the purpose of this recipe, it will be a good idea to give the theme a unique and pertinent name based on its design and use. It is also important to ensure that there are no name-conflicts with other existing core or contributed themes.

How to do it...

A sub-theme of a core theme can be created through the following procedure:

  1. Create a file named mytheme.info inside the mytheme folder.

  2. Edit this new file and add the following code inside it:

    name = Mytheme
    description = My new sub-theme (CSS, phptemplate, 3-col)
    base theme = garland
    core = 6.x
    engine = phptemplate
    stylesheets[all][] = mytheme.css
    

    Note

    It is useful to add an informative...

Overriding base theme elements in a sub-theme


This recipe details the steps involved in overriding a base theme template file. We will be restructuring the layout of a Drupal node by modifying the node.tpl.php template.

Getting ready

We will be using the mytheme sub-theme which was created in the previous recipe.

How to do it...

As we are dealing with a sub-theme here, it is by default relying on the template files of its base theme. To override the base file used to theme the layout of a node, copy the node.tpl.php file from the base theme's folder—themes/garland—to the sites/themes/mytheme folder. Opening the new file in an editor should bring up something similar to the following:

<?php
// $Id: node.tpl.php,v 1.5 2007/10/11 09:51:29 goba Exp $
?>
<div id="node-<?php print $node->nid; ?>" class="node<?php if ($sticky) { print ' sticky'; } ?><?php if (!$status) { print ' node-unpublished'; } ?>">
<?php print $picture ?>
<?php if ($page == 0): ?>
...

Changing the screenshot image of a theme


This recipe details the steps involved in changing the screenshot image associated with a theme. This image provides the user with a preview of what the site will look like when the theme is enabled. This is normally only required when we are working with a sub-theme or a custom theme.

Getting ready

Once the theme is just about ready to go, visit the front page of the site to take the screenshot. Since we are providing a snapshot of the theme, temporarily swap the name of the site with the name of the theme. It might also be useful to prepare some example content for display on the front page to obtain an accurate representation of the style and layout of our theme.

How to do it...

Adding a screenshot for our theme can be done via the following steps:

  1. On the front page, press ALT + Print Screen to take a screenshot of the active window.

    Note

    Mac users can use Command + Shift + 3 while Linux users should be able to bring up the screenshot utility relevant...

Including a CSS file in a theme


This recipe details the steps involved in adding a CSS file to the theme via its .info file. In this case, we will be adding a CSS file to the mytheme sub-theme which we created earlier in this chapter.

Getting ready

Create a CSS file inside the theme's folder named mytheme.css and add the following example rule to it:

* {
color: #996633 !important;
}

This rule should override and change the color of all text on the page to a brownish hue.

How to do it...

Adding a CSS file to a theme is best accomplished via its .info file. Navigate to the theme's folder at sites/all/themes/mytheme and open the mytheme.info file in an editor. Add the following line to this file to include our CSS:

stylesheets[all][] = mytheme.css

Tip

If the CSS file is stored along with other stylesheets in a sub-folder named css, the syntax would include the relative path to the file as follows: stylesheets[all][] = css/mytheme.css.

Once done, save the file and exit the editor. Since we have modified...

Enabling CSS optimization


CSS optimization in Drupal is accomplished through two steps—aggregation and compression. This optimization provides a significant boost to performance both on the server as well as for the user. This recipe details the steps to be performed to enable this feature in Drupal.

Getting ready

CSS optimization is a requirement only when a site is ready to go live. Until such time, it is recommended that it be left switched off as CSS changes during development will not take effect unless the Drupal cache is cleared.

How to do it...

Optimization and other performance-related features are sequestered within admin/settings/performance (Home | Administer | Site configuration | Performance). This performance configuration page should have a section titled Bandwidth optimizations which should contain options for CSS and Javascript optimization. Look for the setting named Optimize CSS files and set it to Enabled as in the following screenshot:

Tip

Public File system

As the screenshot...

Creating the mysite module to hold our tweaks


In the course of developing our site, we will frequently come across situations where various elements of the site need to be tweaked in PHP using Drupal's APIs. While a lot of theme-specific cases can be stored in template files, certain tweaks which are theme-agnostic require that we store them in a module to ensure that they are available to all themes.

This recipe covers the creation of a module to hold all these bits and pieces.

Getting ready

Create a folder inside sites/all named modules. This is where custom and contributed modules are usually housed.

How to do it...

The following list details the procedure involved in creating a module named mysite to hold our theme-agnostic customizations and other odds and ends:

  1. Create a folder inside sites/all/modules named mysite where mysite refers to the name of our site.

  2. Create a file named mysite.info within the mysite folder.

  3. Edit this file and add the following code inside:

    name = Mysite
    description...

Adding a CSS file from a module


Situations arise where CSS files or CSS rules are to be added from a module. This recipe covers the steps required to do so.

Getting ready

We will be adding the CSS to the mysite module as created in the previous recipe. Create two CSS files named mysite.css and mysite_special.css inside the mysite module's folder and populate them with some sample rules.

How to do it...

Add the following code to the mysite module:

/**
* Implementation of hook_init().
*/
function mysite_init() {
// The path to the mysite module.
$path = drupal_get_path('module', 'mysite');
// Include mysite.css.
drupal_add_css($path . '/mysite.css');
// Include mysite_special.css, but do not preprocess.
drupal_add_css($path . '/mysite_special.css', 'module', 'all', FALSE);
}

Save the file and visit the site to check if the two CSS files are now included.

How it works...

The drupal_add_css() function is used to add CSS files from a module. Its syntax is as follows:

drupal_add_css($path = NULL, $type...

Displaying a different theme for each day of the week


This recipe provides the PHP code that allows the rotation of themes based on the day of the week.

Getting ready

As we have seen in other recipes in this chapter, a number of sites use an "odds and ends" module to handle tweaks and customizations particular to the site. We will be using the mysite.module created earlier in this chapter to hold our customizations. It is assumed that the module is available and already enabled.

How to do it...

Open the mysite module file and paste the following code into it:

/**
* Implementation of hook_init().
*/
function mysite_init() {
global $custom_theme;
// An array of themes for each day of the week.
// These themes do not have to be enabled.
$themes = array();
$themes[0] = 'garland';
$themes[1] = 'minnelli';
$themes[2] = 'bluemarine';
$themes[3] = 'pushbutton';
$themes[4] = 'chameleon';
$themes[5] = 'marvin';
$themes[6] = 'mytheme';
// Get the current day of the week in numerical form.
$day = date("w...

Creating a fresh look using the color module


The color module allows the administrator to easily change the color scheme of themes that support it. This facilitates a fresh look for the site without having to create a new theme or sub-theme. Drupal's default theme, Garland, supports the color module and, in this recipe, we will be covering the steps required to change its color scheme to something unique.

Getting ready

Ensure that the color module is enabled in admin/build/modules (Home | Administer | Build | Modules). Since we are going to change the color scheme of the Garland theme, ensure that it is set as the current theme.

How to do it...

Colorizing the Garland theme can be accomplished by following these steps:

  1. Navigate to admin/build/themes (Home | Administer | Build | Themes).

  2. Look for the Garland theme and click on the configure link next to it.

  3. Look for the section titled Color scheme.

  4. In this section, the Color set drop-down lists available presets. Choosing any of the presets will change...

Left arrow icon Right arrow icon

Key benefits

  • Take control of the look and feel of your Drupal website
  • Tips and tricks to get the most out of Drupal's theming system
  • Learn how to customize existing themes and create unique themes from scratch
  • Part of Packt's Cookbook series: Each recipe is a carefully organized sequence of instructions to complete the task as efficiently as possible

Description

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.

Who is this book for?

This book is written for Drupal developers who want to refresh the look and feel of their sites. If you are a Drupal site administrator who is looking to go beyond the basics and customize the presentational aspects of your Drupal site, then this book is for you. It assumes that readers are familiar with rudimentary PHP and acquainted with Drupal installation and general usage. Readers are also expected to have knowledge of CSS and XHTML.

What you will learn

  • Beef up your knowledge of the basics of Drupal theming such as installing and enabling a downloaded theme, changing the logo, and adding a slogan to the theme
  • Customize the look of your website by configuring and extending your existing themes with sub-themes
  • Create custom themes from scratch for websites that demand a fresh look
  • Use the PHPTemplate theming engine to override module output and style it using a theme
  • Style and manipulate the content of your website by adding CSS and JavaScript files via themes and modules
  • Use the ImageCache module to manipulate images on the fly
  • Jazz up the design of your site by incorporating JavaScript elements
  • Make your Drupal site stand out from the crowd by customizing Views via the theme layer
  • Design complex site layouts rapidly with Panels
  • Speed-up development and debugging with Drupal tools such as the Devel module
  • Customize your Drupal site in a creative style by making thorough use of Drupal s APIs

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Nov 12, 2010
Length: 384 pages
Edition : 1st
Language : English
ISBN-13 : 9781847198693
Languages :
Concepts :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Nov 12, 2010
Length: 384 pages
Edition : 1st
Language : English
ISBN-13 : 9781847198693
Languages :
Concepts :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 152.97
Drupal 6 Theming Cookbook
$48.99
Drupal 7 Social Networking
$48.99
OpenVPN 2 Cookbook
$54.99
Total $ 152.97 Stars icon
Banner background image

Table of Contents

12 Chapters
Drupal Theme Basics Chevron down icon Chevron up icon
Beyond the Basics Chevron down icon Chevron up icon
Custom Themes and Zen Chevron down icon Chevron up icon
Templating Basics Chevron down icon Chevron up icon
Development and Debugging Tools Chevron down icon Chevron up icon
Advanced Templating Chevron down icon Chevron up icon
JavaScript in Themes Chevron down icon Chevron up icon
Navigation Chevron down icon Chevron up icon
Form Design Chevron down icon Chevron up icon
Customizing CCK Chevron down icon Chevron up icon
Views Theming Chevron down icon Chevron up icon
Rapid Layouts with Panels Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
(2 Ratings)
5 star 0%
4 star 100%
3 star 0%
2 star 0%
1 star 0%
Paul Crisp Mar 22, 2013
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
I've tried quite a lot of Drupal manuals and this is definitely the best I've found. Theming is a bit of a dark art with multiple interacting components. The Cookbook covers a lot of territory but does so very clearly. It also manages to show exactly how to accomplish something AND explain what your doing and how it works. Probably not geeky enough for a Drupal Master and does require a bit basic knowledge about php and CSS but a good allrounder could cope fine with this. Recommended
Amazon Verified review Amazon
Geoffrey G. Hankerson Feb 10, 2011
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
In Drupal or any Web development environment, the themer's job is to make the site look nice. This might mean making a Photoshop mockup into a real site, or implementing a design in another way. A themer is expected to use CSS, JavaScript and some Php to make this happen.This cookbook offers recipes to help the themer do his job. The recipes start simple with tips on how to download a contributed theme from Drupal.org and install it. The recipes then quickly move to show you how to make a custom theme from scratch or using Zen as a base theme. One recipe walks you through the components of a theme step by step so you can get started on your own theme. The recipes build from there.Some of the other basic recipes I like include: customizing a node template for a specific node type, including a CSS of javascript file in a theme, and optimizing CSS and Javascript for production sites.I also really appreciated the fact that the author encourages the use of a custom module for theme customizations. A lot of themers get caught up in working their theme's directory only and never realize the advantage of using a module to add javascript to a specific page, change a form submit button into an image, etc... (The advantage is the code is more easily reused on another site).What I especially like about the book is the fact that there are several good recipes for experieced themer's like me in addition to basic recipes. Some of the more advanced tips I looked included using Firebug's console.trace for JavaScript debugging, making Php variables available to JavaScript, and even a simple example of a Views Style plugin. I've been wanting to learn to make views style plugins to expose some of those cool jquery effects out there in the wild. This gives me great starting point.The one thing I wish the book would emphasize more is most of implementing a theme still is writing CSS. Often the point of a fancy theme preprocess function is to add a CSS ID or class to some part of the markup so we can target it properly with CSS. This is however a small gripe.This cookbook along with "Front End Drupal" are an essential part of any Pro Themer's library.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.