Categories

(83)
(69)
(8)
(34)
(74)
(149)

Integration With Features Module

28.12.2012
Features Module
Author:

There may be instances when a web developer needs to transfer some database changes from one site to another. Moving the whole database may seem problematic or even become impossible given the new content in the database has appeared. The module Features is essential remedy for this problem. In this article, we will describe how to create your own feature, how to create your own items in the feature and how to export the parts of the site to the other site.

There are three values in the database (later on we will create them with a help of function variable_set()) which should be moved over to some other site.

With a help of the simple steps described below we will show you how to choose the values you need to export and the values you do not need.

1) Install the module Features.

2) Create your own module.

We will name our module idevels.

In the file idevels.module add hook_features_api(). With a help of this hook we inform about our feature and implement some parameters.

/**
 * Implements hook_features_api().
 */
function idevels_features_api() {
  return array(
    'values' => array(
      'name' => t('Deploy values'),
      'default_hook' => 'values_defaults',
      'default_file' => FEATURES_DEFAULTS_INCLUDED,
      'feature_source' => TRUE,
      'file' => drupal_get_path('module', 'idevels') .'/idevels.features.inc',
    ),
  );
}

Now in the root of our module we create a file which was indicated in hook_features_api, in our case idevels.features.inc.

In this file, add hook hook_features_export_options().

At this point, we have to take heed of some important moment. Hooks that are involved in the work of our feature should be given the name after the name of the feature, not after the module's name. In this particular case it would be values.

/**
 * Implements hook_features_export_options().
 */
function values_features_export_options()  {
  //only create variables
  variable_set('value1', 'value1');
  variable_set('value2', 'value2');
  variable_set('value3', 'value3');
  
  return array(
    'value1' => t('VALUE 1'),
    'value2' => t('VALUE 2'),
    'value3' => t('VALUE 3'),
  );
} 

To make it more obvious, we will create three values with the help of function variable_set(). In hook_features_export_options() we set options which a user can move. In our case it looks like that:

Then add hook_features_export() that would show what we export choosing a particular option.
 /**
 * Implements of hook_features_export().
 */
function values_features_export($data, &$export, $module_name = '') {
  foreach ($data as $name) {
    $export['features']['values'][$name] = $name;
  }
  return array();
}

When we choose any option now, we see what is going to be exported. In our case it looks like that:

Now add hook_features_export_render() that renders changes being exported into the file.

/**
 * Implements hook_features_export_render().
 */
function values_features_export_render($module_name, $data) {
  $code = array();
  $values = array();
  foreach ($data as $name) {
    $values[$name] = variable_get($name);
  }
  $code = '  $data = ' . features_var_export($values, '  ') . ';' . PHP_EOL;
  $code .= "  return \$data;";
  return array('values_defaults' => $code);
}

The two hooks still to be described are hook_features_rebuild() and hook_features_revert(). They would be responsible for data entry on a new site from our feature. In our case they would do the same operation, that is why they look like that:

/**
 * Implements hook_features_rebuild().
 */
function values_features_rebuild($module) {
  values_features_revert($module);
}

/**
 * Implements hook_features_revert().
 */
function values_features_revert($module) {
  $data = module_invoke($module, 'values_defaults');
  foreach ($data as $name => $value) {
    variable_set($name, $value);
  }
}

Thus, these two hooks would change the data on the site to those that are contained in our feature. Simple as that, we showed how to export the parts of the site to another site. The module that our Drupal development company created in this article is attached below.

19 votes, Rating: 3.9

Read also

1

As social networks gain more and more ground customers are often confronted with the task of integrating their sites with Facebook, Twitter, Google+, etc. ...

2

It is common knowledge that Google Analytics (hereinafter GA) is a powerful tool for collecting data about user...

3

Queue API is a particular functional in Drupal that allows you to line up all labor-intensive operations and further check up their implementation on the site. Unlike Batch API,...

4

This article is about hot quickly to configure the module Search API and Search API Solr search, and how to display the data from Solr server with the help of the module Views.

5

Our company often uses the Panels module for creating sites. Although this option adds a considerable amount of HTML structures, it provides us with a flexible, user-friendly, and easy-to-operate...

Subscribe to our blog updates