You are here

function field_nif_update_7100 in Field NIF 7

Implements hook_update_N(). Update the schema of all nif fields.

File

./field_nif.install, line 45

Code

function field_nif_update_7100(&$sandbox) {

  // Get all fields
  $fields = field_info_fields();
  foreach ($fields as $field_name => $field) {
    if ($field['type'] == 'nif') {

      // Get all schema and replace whith new
      $schema = _field_sql_storage_schema($field);
      $schema['field_data_' . $field_name]['fields'][$field_name . '_number'] = array(
        'description' => 'Numeric part of the NIF',
        'type' => 'varchar',
        'length' => 8,
      );
      $schema['field_revision_' . $field_name]['fields'][$field_name . '_number'] = array(
        'description' => 'Numeric part of the NIF',
        'type' => 'varchar',
        'length' => 8,
      );

      // Get field data
      $result_data = db_select('field_data_' . $field_name, 'n')
        ->fields('n')
        ->execute()
        ->fetchAll();
      $result_revision = db_select('field_revision_' . $field_name, 'n')
        ->fields('n')
        ->execute()
        ->fetchAll();

      // Rename old tables
      db_rename_table('field_data_' . $field_name, 'field_data_' . $field_name . '_old');
      db_rename_table('field_revision_' . $field_name, 'field_revision_' . $field_name . '_old');

      // Create new tables with new schema
      db_create_table('field_data_' . $field_name, $schema['field_data_' . $field_name]);
      db_create_table('field_revision_' . $field_name, $schema['field_revision_' . $field_name]);

      // Restore field data
      foreach ($result_data as $r) {
        db_insert('field_data_' . $field_name)
          ->fields(array(
          'entity_type' => $r->entity_type,
          'bundle' => $r->bundle,
          'deleted' => $r->deleted,
          'entity_id' => $r->entity_id,
          'revision_id' => $r->revision_id,
          'language' => $r->language,
          'delta' => $r->delta,
          $field_name . '_number' => $r->{$field_name . '_number'},
          $field_name . '_first_letter' => $r->{$field_name . '_first_letter'},
          $field_name . '_last_letter' => $r->{$field_name . '_last_letter'},
          $field_name . '_type' => $r->{$field_name . '_type'},
        ))
          ->execute();
      }
      foreach ($result_revision as $r) {
        db_insert('field_revision_' . $field_name)
          ->fields(array(
          'entity_type' => $r->entity_type,
          'bundle' => $r->bundle,
          'deleted' => $r->deleted,
          'entity_id' => $r->entity_id,
          'revision_id' => $r->revision_id,
          'language' => $r->language,
          'delta' => $r->delta,
          $field_name . '_number' => $r->{$field_name . '_number'},
          $field_name . '_first_letter' => $r->{$field_name . '_first_letter'},
          $field_name . '_last_letter' => $r->{$field_name . '_last_letter'},
          $field_name . '_type' => $r->{$field_name . '_type'},
        ))
          ->execute();
      }

      // Delete old tables
      db_drop_table('field_data_' . $field_name . '_old');
      db_drop_table('field_revision_' . $field_name . '_old');
    }
  }
  field_cache_clear();
}