How to create .info file for themes in Drupal 7
Blog

How to create .info file for themes in Drupal 7

The .info files is most important part of Drupal themes. The .info file in a Drupal theme configures the theme. Drupal 7 Support PHPTemplate engine which is needed to powered theming system. This file has a configuration function and syntax similar to a .ini file. 

The .info file is a collection of static text file, used to define and configuring a theme. Each line in the .info file has a key-value pair with the key on the left and value on the right hand side, with an "equals sign" between them (e.g. name = my_new_theme). Semicolons are used to comment out a line. As we do follow # symbol or /* */ symbols to comment out or to provide developer comment. Some keys uses a syntax with square brackets for building a list of associated values, represent to as an "array". If you are not aware with arrays, have a look at the default .info files that come with Drupal and read the explanations of the examples that follow. Even though the .info file is not natively opened by an Application, you can use TextEdit or Notepad on a Windows computer in order to view, edit, and save your changes.

Let’s look at an example of what you might have found in an .info file

name = My Theme
description = A description of my theme
core = 7.x

# Add a base theme, if you want to use one.
base = mybasetheme

# Define regions, otherwise the default regions will be used.

regions[header] = Header
regions[navigation] = Navigation
regions[content] = Content
regions[sidebar] = Sidebar
regions[footer] = Footer

 

# Define which features are available. If none are specified, all the default 
# features will be available.

features[] = logo
features[] = name
features[] = favicon

# Add stylesheets

stylesheets[all][] = css/reset.css
stylesheets[all][] = css/mytheme.css
stylesheets[print][] = css/print.css

 

# Add javascript files

styles[] = js/mytheme.js

 

 

There are some of the most needed information that need to be in every Drupal .info file

name – This should be human-friendly name that will be displayed on the theme select page.
and it can be set independently from the internal "machine" readable name. This imposes fewer restrictions on the allowed characters.

name =  A fancy theme

description (recommended) – This is a short description of the theme that helps identifying it on the theme select page. This description is displayed on the theme select page at "Administer > Site building > themes".

description = multi column row for fancy theme

core – earlier version of Drupal 6.x .info file should state which major core version of Drupal it is compatible with, just  to avoid compatibility issues. The value set here is compared with the DRUPAL_CORE_COMPATIBILITY constant. If it does not match, the theme will be disabled.

But in Drupal 7.x packaging script automatically sets this value based on the Drupal core compatibility setting on each release node. So people downloading packaged themes from drupal.org will always get the right thing. 

On top of that we also have other key value pair which is needed optionally.

core= 7.x

version (discouraged)
The version string is automatically be added by drupal.org when a release is created and a tarball packaged. So you may omit this value for contributed themes. However, if your theme is not being hosted on the drupal.org infrastructure, you can give your theme whatever version string makes sense.

version = 1.0 

base theme
When you are about to create a new theme in that scenario Sub-themes can declare a base theme. This allows theme to inherit, reusing the resources from the "base theme" will cascade and be reused inside the sub-theme. Sub-themes can declare other sub-themes as their base, allowing multiple levels of inheritance. Use the internal "machine" readable name of the base theme. 

base theme = bootstrap

region
If you overriding regions in D7, you are obliged to declare the line regions[content] = Content. If you want any of the default regions in your theme, you have to declare them as well.

In Drupal, we have block regions. Block regions are dynamic content areas that hold different content (block) that you can set through the Drupal administrator dashboard. The important thing is, these regions are set in the .info file of a Drupal theme.

regions[left] = Left sidebar
regions[right] = Right sidebar
regions[content] = Content
regions[header] = Header
regions[footer] = Footer

The above is a list of default regions in Drupal 7. To use those regions in the theme’s page.tpl.php file, all you need to do is something similar to this:

/* Display blocks in "Left sidebar" */

Features
In Drupal 7, features are not inherited from the base theme. If you are using features other than the default, you should copy the features declarations from the parent theme's .info file.
Various page elements output by the theme can be toggled on and off on the theme's config page. The "features" keys control which of these check boxes display on the theme's config page. 

The example below lists all the available elements controlled by the features key.

Drupal 7 features

features[] = logo 
features[] = name 
features[] = slogan 
features[] = node_user_picture 
features[] = comment_user_picture 
features[] = comment_user_verification 
features[] = favicon 
features[] = main_menu 
features[] = secondary_menu 


Stylesheets
To Override inherited style sheets: Specify a stylesheet with the same filename in the sub-theme. For instance, to override style.css inherited from a parent theme, add the following line to your sub-theme's .info file:

stylesheets[all][] = style.css

On drupal 6.x version themes default to using style.css automatically and could add additional stylesheets by calling drupal_add_css() in their template.php file. Whereas in Drupal 7, themes no longer default to using style.css if it is not specified in the .info file.


Scripts
All JavaScript files defined in the parent theme will be inherited.

Overriding inherited JavaScript: Specify a JavaScript file with the same filename in the sub-theme's .info file. For instance, to override script.js inherited from a parent theme, add the following line to your sub-theme's .info file:

On drupal 6.x version themes could add Javascripts by calling drupal_add_js() in their template.php file. if a file named script.js exists in the theme directory then it is automatically included. However, in Drupal 7.x version, this behavior has been changed again so that script.js is only included if it has been specified in the .info file:

scripts[] = myscript.js 

Conclusion: Drupal theme development could be a bit complicated at first time, but learning about Drupal themes in general and learning about what each file does and How functionality work and what type of function call does it make could help you a lot to set you on the right direction to becoming a Drupal Front End developer.
 

Download the Drupal Guide
Enter your email address to receive the guide.
get in touch