You are here

Using Features' component alter hooks to export components with dynamic elements

Note: These comments are based on Drupal 6, although the hooks are also available in Drupal 7. The Features module is a great development tool that allows you to grab components stored in the database of your local development environment and export them to code so they can be transferred to other environments. Using Features to export content types, cck fields, views, feeds importers, imagecache presets, and many other components and settings has made development and deployment a lot smoother. Even better was learning about the hooks that allow you to alter the default components exported by features. Here are a couple of situations where these hooks provide just the fix needed.

Problem: Hard-coded vids, tids and rids in exported views

An exported view can contain hard-coded vocabulary vids, term tids or role rids which may not correspond to the same vocabularies, terms or roles in a different environment. If you change the code in the hook_views_default_views() function implemented by features, the code will be overwritten anytime you update the feature and you would have to make those same changes over again each time.

Solution: hook_views_default_views_alter()

Don't make any changes to the hook_views_default_views() function in the file created by features. Instead add a hook_views_default_views_alter()function in the .module file and make your changes there. (While other alter hooks are provided by the features module, the views module provides this one.) If you store your id in the variables table when you create your vocabulary, term or role, then you can retrieve it from the variables table and in any environment it will refer to the correct vocabulary, term or role.

define('MYMODULE_MYVOCAB_VID', variable_get('my vocab vid', null));

/**
 * Implementation of hook_views_default_views_alter().
 */
function mymodule_views_default_views_alter(&$views) {
  if (isset($views['view_name'])) {
    $views['view_name']->display['block_1']->display_options['filters']['vid']['value'] = array(MYMODULE_MYVOCAB_VID => MYMODULE_MYVOCAB_VID);
  }
}

Problem: Hard-coded arguments for quicktab views

I was exporting a quicktabs block containing views and I wanted to pass an argument to each view based on the current date. Features exports quicktabs blocks using hook_quicktabs_default_quicktabs() but any arguments for included views are hard-coded.

Solution: hook_quicktabs_alter() (patch required)

You'll have to apply the patch filed against this issuebefore implementing this hook. Once again, place this hook in the .module file of your feature module and you can access any of the quicktab settings you would like to change.

/**
 * Implementation of hook_quicktabs_alter().
 */
function mymodule_quicktabs_alter(&$quicktab, $op) {
	if ($quicktab->machine_name == 'my_block' && $op == 'view') {
    // Some code to set default tab ($default_tab_key)
    // and an array of arguments for each tab ($date_args)
    
    // Set default tab and date args for all tabs.
    $quicktab->default_tab = $default_tab_key;
    foreach ($quicktab->tabs as $key => $quicktab) {
      $quicktab->tabs[$key]['args'] = $date_args[$quicktab['title']];
    }
  }
}

Problem: CCK content taxonomy field with hard-coded vid

One last example, same idea. The content taxonomy field exported by views uses a hard-coded vid but I want to set the vocabulary dynamically.

Solution: hook_content_default_fields_alter()

Use this hook to alter any of the settings exported by features in hook_content_default_fields() in the mymodule.features.content.inc file.

/**
 * Implementation of hook_content_default_fields_alter().
 */
function mymodule_content_default_fields_alter(&$fields) {
  if (array_key_exists('my_node_type-field_my_cck_field', $fields)) {
    $vid = //code to get vid...
    $fields['my_node_type-field_my_cck_field']['vid'] = $vid;
  }
}

To see all the component alter hooks available go to http://drupalcontrib.org/api/drupal/contributions--features--features.api.php/group/features_component_alter_hooks/6 or http://api.lullabot.com/group/features_component_alter_hooks.

Appnovation Technologies