You are here

function field_features_rebuild in Features 7

Same name and namespace in other branches
  1. 7.2 includes/features.field.inc \field_features_rebuild()

Implements of hook_features_rebuild(). Rebuilds fields from code defaults.

1 call to field_features_rebuild()
field_features_revert in includes/features.field.inc
Implements hook_features_revert().

File

includes/features.field.inc, line 162

Code

function field_features_rebuild($module) {
  if ($fields = features_get_default('field', $module)) {
    field_info_cache_clear();

    // Load all the existing fields and instance up-front so that we don't
    // have to rebuild the cache all the time.
    $existing_fields = field_info_fields();
    $existing_instances = field_info_instances();
    foreach ($fields as $field) {

      // Create or update field.
      $field_config = $field['field_config'];
      if (isset($existing_fields[$field_config['field_name']])) {
        $existing_field = $existing_fields[$field_config['field_name']];
        if ($field_config + $existing_field != $existing_field) {
          field_update_field($field_config);
        }
      }
      else {
        field_create_field($field_config);
        $existing_fields[$field_config['field_name']] = $field_config;
      }

      // Create or update field instance.
      $field_instance = $field['field_instance'];
      if (isset($existing_instances[$field_instance['entity_type']][$field_instance['bundle']][$field_instance['field_name']])) {
        $existing_instance = $existing_instances[$field_instance['entity_type']][$field_instance['bundle']][$field_instance['field_name']];
        if ($field_instance + $existing_instance != $existing_instance) {
          field_update_instance($field_instance);
        }
      }
      else {
        field_create_instance($field_instance);
        $existing_instances[$field_instance['entity_type']][$field_instance['bundle']][$field_instance['field_name']] = $field_instance;
      }
    }
    if ($fields) {
      variable_set('menu_rebuild_needed', TRUE);
    }
  }
}