The Features module is an outstanding solution for exporting module or site configuration that resides in the database as source code within a module. This has the following benefits:
You should have basic skills of using the Features module, through the administration interface, in order to make the most out of the following examples. A good place to start is at the documentation home page at http://drupal.org/node/580026.
In this example, we will create a view, add it to a feature, change the view, and see how the feature reflects this change. Here are the steps:
page
with the title Simple Node Listing.$ drush pm-download features diff
Project features (7.x-1.0-beta6) downloaded to [success] /home/juampy/projects/drupal/sites/all/modules/contrib/features.
Project diff (7.x-2.0) downloaded to [success] /home/juampy/projects/drupal/sites/all/modules/contrib/diff.
$ drush pm-enable --yes features diff
The following extensions will be enabled: features, diff
Do you really want to continue? (y/n): ydiff was enabled successfully. [ok]
features was enabled successfully. [ok]
features-export
command, which accepts a feature name and a list of components to add to it. We will first list the available feature components to find the one with the name of our view:$ drush features-export
...
views_view:frontpage
views_view:glossary
views_view:comments_recent
views_view:simple_node_listing
views_view:taxonomy_term
views_view:tracker
dependencies:block
...
views_view:simple_node_listing
. Now, we can call the features-export
command to create a feature called feature_node_listing:
$ drush features-export feature_node_listing views_view:simple_node_listing
Created module: feature_node_listing in [ok] sites/all/modules/feature_node_listing
sites/all/modules/feature_node_listing
, you will see that it holds all the settings of the view Simple Node Listing
. This will be used later on to track changes on its configuration. feature-list
command, we will see our feature:$ drush features-list
Name Feature Status State
feature_node_listing feature_node_listing Disabled
Features Tests features_test Disabled
Disabled
. We will enable it like we would do for a normal module so we can track its State:
$ drush pm-enable feature_node_listing
The following extensions will be enabled: feature_node_listing
Do you really want to continue? (y/n): y
feature_node_listing was enabled successfully. [ok]
$ drush features-list
Name Feature Status State
feature_node_listing feature_node_listing Enabled
Features Tests features_test Disabled
Simple Node Listing
view to see how the State
changes. Go to Structure | Views | Edit Simple Node Listing view and change the page display name to Node Listing. Save your changes and then in the command line list all features again as in the following example:$ drush features-list
Name Feature Status State
feature_node_listing feature_node_listing Enabled Overridden
Features Tests features_test Disabled
Overridden
state. This means that the configuration in the database does not match with the one in the feature code. As we have installed the Diff module, Features can print a list of differences with the features-diff
command. the following are the current differences between the Simple Node Listing
view in the database and the one in our feature:$ drush features-diff feature_node_listing
Legend:
Code: drush features-revert will remove the overrides.
Overrides: drush features-update will update the exported feature with the displayed overrides
Component: views_view
);
< /* Display: Page */
< $handler = $view->new_display('page', 'Page', 'page'),
---
> /* Display: Node listing */
> $handler = $view->new_display('page', 'Node listing', 'page'),
$handler->display->display_options['path'] = 'simple-node-listing';
,
)
diff
command from Linux. We can clearly see in the highlighted lines that, currently, the title of the page display is Node listing
in the database, while in the feature module it is Page
. Overridden
state, we can either revert it or update it. Reverting a feature means to push what is in the source code to the database (thus, setting the title back to Page)
, while updating a feature means pulling what is in the database into source code (hence keeping the page title to Node Listing)
. Both actions will set the feature back to a default state. Here is how we could update it using features-update:
$ drush features-update --yes feature_node_listing
Module appears to already exist in sites/all/modules/feature_node_listing
Do you really want to continue? (y/n): y
Created module: feature_node_listing in [ok]
sites/all/modules/feature_node_listing
features-revert
undoes the changes we did on the view:$ drush features-revert --yes feature_node_listing
Do you really want to revert views_view? (y/n): y
Reverted views_view. [ok]
Overridden
status:$ drush features-list
Name Feature Status State
feature_node_listing feature_node_listing Enabled
Features Tests features_test Disabled
Once a feature module has been created, we can still add more components to it. Imagine, that we want to add the permission to administer views to our existing feature. We will first list the available feature components by giving the feature name to the features-add
command and then add it:
$ drush features-add feature_node_listing
Available components
...
user_permission:change own username
user_permission:cancel account
user_permission:select account cancellation method
user_permission:administer views
user_permission:access all views
views_view:archive
views_view:backlinks
...
$ drush features-add feature_node_listing "user_permission:administer views"
Module appears to already exist in sites/all/modules/feature_node_listing
Do you really want to continue? (y/n): y
Created module: feature_node_listing in [ok]
sites/all/modules/feature_node_listing
The previous command added the permission to administer views to our feature. Note that, as the feature component name has spaces, we wrapped it with quotes so Drush will not take it as two different feature components. If you view the source code of sites/all/modules/feature_node_listing
, you will see that it holds both (the view and the permission).
The Features module is normally used to propagate configuration and data to other Drupal instances and to highlight when a component has changed. Module configuration can be added to a feature by installing the Strongarm module (http://drupal.org/project/strongarm) and a fine-grain level of component picking can be achieved with the Features Plumber module (http://drupal.org/project/features_plumber). There are many other modules which support or extend Features which can be found at http://drupal.org/project/modules by searching the Features Package category.
18.224.59.192