You are here

function content_features_rebuild in Features 6

Implementation of hook_features_rebuild(). Rebuilds CCK field definitions from code defaults.

1 call to content_features_rebuild()
content_features_revert in includes/features.content.inc
Implementation of hook_features_revert().

File

includes/features.content.inc, line 149

Code

function content_features_rebuild($module) {
  if ($fields = features_get_default('content', $module)) {
    module_load_include('inc', 'content', 'includes/content.crud');
    content_clear_type_cache(TRUE);
    foreach ($fields as $field) {

      // We use content_field_instance_read() here so that we can get inactive
      // fields too. We can't just pass $field to it as the fnc will add every
      // item of the array to the WHERE clause.
      $param = array(
        'field_name' => $field['field_name'],
        'type_name' => $field['type_name'],
      );
      $read = content_field_instance_read($param, TRUE);
      $existing_instance = array_shift($read);
      if (!empty($existing_instance)) {

        // If this existing instance is inactive, we need to activate it before
        // running content_field_instance_update().
        if (empty($existing_instance['widget_active'])) {
          db_query("UPDATE {" . content_instance_tablename() . "} SET widget_active = 1 WHERE field_name = '%s' AND type_name = '%s'", $field['field_name'], $field['type_name']);

          // We need to clear the type cache again, unfortunately.
          content_clear_type_cache(TRUE);
        }
        content_field_instance_update($field, FALSE);
      }
      else {

        // Summary: content_field_instance_create() will fall back to prior
        // instance values for weight, label, desc if not set.
        // See http://drupal.org/node/686240#comment-2786162
        $field['weight'] = $field['widget']['weight'];
        $field['label'] = $field['widget']['label'];
        $field['description'] = $field['widget']['description'];
        content_field_instance_create($field, FALSE);
      }
      variable_set('menu_rebuild_needed', TRUE);
    }
  }
}