Drupal “Features” Module Introduction

The Features module is a popular module for Drupal 6 and Drupal 7. What does the features module do? The Features module was originally designed for a programmer to create a Feature or piece of a website that is reusable. An example of this may be a multi-user blog section of a website: its content type, permissions, url paths … all the configuration needed for a cookie-cutter blog area of a website could be a “Feature” of a website. To facilitate this idea the Kit Specification was created a long time ago.

I don’t often see Features used in this way. Typically, now-a-days Features is used to encapsulate Configuration and provide a mechanism to move code from 1 server to another — it’s a sane way to distribute your code between servers.

In Drupal 8 this configuration encapsulation has largely been implemented as the Configuration Management Initiative (CMI). In Drupal 8 it should be much easier to move pieces of Drupal configuration between development, testing and production servers as an example.

What is a Feature Then?

A “Feature” is just a Drupal Module. A Feature contains additional .info file directives that inform the Features module of what configuration Features’ is responsible for. An example Features .info file entries are:

In the above info file you see the features keyword and it controls (exported) values for various site items such as Permissions, Roles, the contrib module Boxes, Input Filters, a CKEditor profile. Features typically references the machine_name of an item.

When you make a Feature through the Admin UI or using Drush it creates a module which in the .module contains at least 1 line of code at the top of the file:

include_once(‘mymodule.features.inc’);

This allows Features to divine its responsibilities in your custom module. Additionally features may make additional *.features.inc files in your modules directory. If you export Field Configuration for example you will see mymodule.features.field.inc. If you export User Roles you will also see a mymodule.user_role.inc file. If a 3rd party module supports Feature’s Exportabability its features inc file is typically mymodule.features.CONTRIB_MODULE_NAME.inc.

These include files typically contain drupal, or contrib module Hooks that allow Features to create, alter and/or define an item of configuration.

This is a Features hook to build fields — it’s basically defining array’s used in Drupal core to re-create your Field with field_create_field() and field_create_instance() with any and all configured FieldUI values for that field — and where in your drupal website it’s used (what Content Types or Entities).

The Features module itself provides an API with its own set of Hooks to alter, define and edit a Feature during its lifecycle. See the API.txt that ships with Features.

Using Features:

Typically you use the Features UI in the Drupal Administration area to export your feature. Or you may use Drush. Once you have a folder of files (your module) — you may move it between systems.

Features look at the .info file contents to derive what to do. If you add an entry that says Features controls some Field — you can use Drush and the command features-update to have Features export the related configuration to a Feature’s file code.

If you have a Feature and you’ve modified the configuration from within the Drupal Admin area — you may revert a Feature to the module’s defined configuration with features-revert.

A concise list of Drush Features options can be found in the help documentation:

$drush help --filter=features
All commands in features: (features)
features-add (fa) Add a component to a feature module. (DEPRECATED: use features-export)
features-components List features components.
(fc)
features-diff (fd) Show the difference between the default and overridden state of a feature.
features-export (fe) Export a feature from your site into a module.
features-list (fl, List all the available features for your site.
features)
features-revert (fr) Revert a feature module on your site.
features-revert-all Revert all enabled feature module on your site.
(fr-all, fra)
features-update (fu) Update a feature module on your site.
features-update-all Update all feature modules on your site.
(fu-all, fua)

Hopefully this overview blog entry on Features helps you to make better use of Features on your site. A good rule of thumb when using Features is a Feature should do 1 thing — not many things.

Author Spotlight

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com