Skip to content

Instantly share code, notes, and snippets.

@jpoesen
Created August 20, 2014 10:33
Show Gist options
  • Save jpoesen/1a1c2b23f27c64c07ff9 to your computer and use it in GitHub Desktop.
Save jpoesen/1a1c2b23f27c64c07ff9 to your computer and use it in GitHub Desktop.
D7 function to revert all features.
function foo_features_revert_all($force = FALSE) {
module_load_include('inc', 'features', 'features.export');
$features_to_revert = array();
foreach (features_get_features(NULL, TRUE) as $module) {
if ($module->status) {
// If forced, add module regardless of status.
if ($force) {
$features_to_revert[] = $module->name;
}
else {
switch (features_get_storage($module->name)) {
case FEATURES_OVERRIDDEN:
case FEATURES_NEEDS_REVIEW:
case FEATURES_REBUILDABLE:
$features_to_revert[] = $module->name;
break;
}
}
}
}
if ($features_to_revert) {
foreach ($features_to_revert as $module) {
if (($feature = features_load_feature($module, TRUE)) && module_exists($module)) {
$components = array();
// Forcefully revert all components of a feature.
if ($force) {
foreach (array_keys($feature->info['features']) as $component) {
if (features_hook($component, 'features_revert')) {
$components[] = $component;
}
}
}
// Only revert components that are detected to be Overridden/Needs review/rebuildable.
else {
$states = features_get_component_states(array($feature->name), FALSE);
foreach ($states[$feature->name] as $component => $state) {
if (in_array($state, array(FEATURES_OVERRIDDEN, FEATURES_NEEDS_REVIEW, FEATURES_REBUILDABLE)) && features_hook($component, 'features_revert')) {
$components[] = $component;
}
}
}
if (!empty($components)) {
foreach ($components as $component) {
features_revert(array($module => array($component)));
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment