diff --git a/features.api.php b/features.api.php index 2f6fcf6..2defc08 100644 --- a/features.api.php +++ b/features.api.php @@ -363,5 +363,20 @@ function hook_user_default_roles_alter(&$roles) { } /** + * Alter the code generated by an exportable. + * + * @param &$code + * By reference. The generated code, as an associative array keyed by the + * function name (default_hook). + * @param $context + * An associative array containing the following keys: + * - module_name: The name of the module code being exported. + * - component: The exportable component name. + * - export: The raw data sent to the renderer. + */ +function hook_features_export_render_alter(&$code, $context) { +} + +/** * @} */ diff --git a/features.export.inc b/features.export.inc index 396bb0f..9cd4b50 100644 --- a/features.export.inc +++ b/features.export.inc @@ -190,6 +190,12 @@ function features_export_render_hooks($export, $module_name, $reset = FALSE) { asort($data); if (features_hook($component, 'features_export_render')) { $hooks = features_invoke($component, 'features_export_render', $module_name, $data, $export); + $context = array( + 'module_name' => $module_name, + 'component' => $component, + 'export' => $export, + ); + drupal_alter('features_export_render', $hooks, $context); $code[$component] = $hooks; } } diff --git a/features.module b/features.module index e49dbb2..9a881b9 100644 --- a/features.module +++ b/features.module @@ -837,6 +837,33 @@ function features_enable_feature($module) { } /** + * Implements hook_features_export_render_alter(). + * + * Add the drupal_alter helper to the exported code to enable override support + */ +function features_features_export_render_alter(&$data, $context) { + if (is_array($data)) { + $alter_line = ' features_alter_code(' . var_export($context['component'], TRUE).', \$$2);'; + foreach ($data as $default_hook => $code) { + // Search for the "return $varname;" at the end of the exportable. + // If it exists, then add our alter helper function just before the return. + $data[$default_hook] = preg_replace( + '/\s+(return\s+\$([a-zA-Z_]+);)/', + "\n\n" . $alter_line . "\n\n " . '$1', + $code); + } + } +} + +/** + * Helper function called at the end of exportable code to add the alter hook + * for modifying the data. + */ +function features_alter_code($component, &$data) { + drupal_alter('features_code', $data, $component); +} + +/** * Utility functions ================================================== */ diff --git a/includes/features.taxonomy.inc b/includes/features.taxonomy.inc index 5ac4ec5..4079075 100644 --- a/includes/features.taxonomy.inc +++ b/includes/features.taxonomy.inc @@ -72,7 +72,10 @@ function taxonomy_features_export_render($module, $data) { } } } - $code = " return ". features_var_export($code, ' ') .";"; + $output = array(); + $output[] = ' $taxonomy = ' . features_var_export($code, ' ') . ';'; + $output[] = ' return $taxonomy;'; + $code = implode("\n", $output); return array('taxonomy_default_vocabularies' => $code); }