Endangers life

Reverting features in update hooks requires a helper function

Description of Erratum

  • The tip on page 258 (Chapter 13, Putting a site online and deploying new features) does not work. The Features revert command needs some helper code wrapping around it, called straight it will fail.

  • Recommended Correction

  • The solution is to steal some code from Drush until Features makes a utility function available. A good place for this code is in the .install file of a custom installation profile for your site (or a master feature) because that is where all site-related updates, such as reverting Features, should go. This would be appropriately named for an example.install file, rename the function from example to the name of the .install file you are reverting features from.

    <?php
    /**
     * Revert specified features.
     *
     * @TODO Check that it really is Forced features revert.  Currently an exact
     * copy of the function initially placed in feature_projects.install.
     *
     * Code mostly taken from drush.
     */
    function sdl_features_revert($modules) {
     
    module_load_include('inc', 'features', 'features.export');
     
    features_include();

      foreach (

    $modules as $module) {
        if ((
    $feature = feature_load($module, TRUE)) && module_exists($module)) {
         
    $components = array();
         
    // Forcefully revert all components of a feature.
         
    foreach (array_keys($feature->info['features']) as $component) {
            if (
    features_hook($component, 'features_revert')) {
             
    $components[] = $component;
            }
          }
        }
        foreach (
    $components as $component) {
         
    features_revert(array($module => array($component)));
        }
      }
    }
    ?>

    Use would be from hook_update_N() functions and look like:

    <?php
    /**
     * Implements hook_update_N().
     *
     * Revert all content-related features, necessary for using correct view mode.
     */
    function example_update_7005() {
     
    sdl_features_revert(array('feature_about', 'feature_awards', 'feature_events', 'feature_news', 'feature_people', 'feature_projects', 'feature_frontend'));
    }
    ?>

    Remember do not use closing PHP (?>) tags in your Drupal files (except templates).

  • Subscribe to Endangers life