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

Installing and enabling a theme


This recipe will cover the steps required to install and enable a downloaded theme.

Getting ready

Downloaded themes are made available in both the ubiquitous zip format as well as the format which usually offers superior compression. These files can be extracted using archive programs such as 7-Zip (http://www.7-zip.org) as well as commercial packages such as WinZip (http://www.winzip.com) and WinRAR (http://www.rarlabs.com).

How to do it...

To install a theme, open Windows Explorer and navigate to the Drupal installation:

  1. 1. Browse to sites/all/themes.

  2. 2. Extract the downloaded theme into a subfolder inside this folder. In other words, if the theme is called mytheme, the folder sites/all/themes/mytheme should contain all the files of the theme:

In the previous screenshot, we see the Sky theme’s installation folder situated within sites/all/themes. Themes also often contain a README.txt file which provides documentation which is worth a read-through.

Note

File structure options

In this recipe, we have chosen to use the folder sites/all/themes/ to store our theme. By positioning our theme inside sites/all, we are stating that the theme is to be available to all sites using this Drupal installation. In other words, this enables multi-site setups to share modules and themes. In case we want to restrict access to the theme solely to one particular site, we would position its folder within sites/foo.example.com/themes/ where foo.example.com is the site in question.

  1. 3. Access the Drupal site in a browser and navigate to admin/appearance [Home | Administration | Appearance].

  2. 4. As in the following screenshot, the newly installed theme should now be listed on this page under the Disabled themes section. Click on the associated Enable and set default link to activate the theme:

How it works...

Drupal scans folders within sites/all/themes and in particular looks for files with the extension .info. These files contain information about each theme such as its name, description, version compatibility, and so on. If the theme is compatible, it is listed on the theme administration page.

A site can have multiple themes enabled. Out of these, only one can be chosen as the default theme. The default theme is, as the name suggests, the primary theme for the website. In the following screenshot, we can see that the Sky theme is now enabled and is the new default theme for the site overriding the core theme, Bartik, which is relegated to second position in the list of enabled themes:

There’s more...

Drupal makes it easier for us to manage our sites by following preset naming conventions when it comes to the folder structure of the site.

Folder structure

Themes do not necessarily have to be placed at the root of the sites/all/themes folder. For organizational purposes, it might be useful to create sites/all/themes/contrib and sites/all/themes/custom. This will allow us to differentiate between downloaded themes and custom themes.

Note

Since Drupal’s core themes are located within the root themes folder, we might be led to believe that this could be a good place to store our contributed or custom themes. While this will certainly work, it will prove to be a bad decision in the long run as it is never a good idea to mix core files with custom files. The chief reason for this separation is manageability. It is far easier to maintain and update Drupal when there is a clear distinction between the core installation and contributed or custom modules and themes. It also ensures that we do not accidentally overwrite or lose our changes when we upgrade our site to the next Drupal release.

Disabling a theme

Enabled themes can be disabled by clicking on their associated Disable links. However, this can only be done if they are not currently the default theme of the site. If the link is missing, then another theme will first need to be set as the default. Once this is done, the Disable link should automatically become available.

See also

Once a theme is enabled, the next logical step would be to configure it. The following recipes in this chapter, namely Uploading a new logo, Uploading a new favicon, and so on describe how to do so.

While this recipe dealt with installing and enabling a downloaded theme, it is also a good idea to consider Creating a subtheme based on a core theme recipe in Chapter 2, Beyond the Basics as well as Creating a theme from scratch recipe in Chapter 3, Custom Themes and Zen.