You are here

field_nif.install in Field NIF 7

File

field_nif.install
View source
<?php

/*
 * @file
 * NIF/CIF/NIE field install file.
 */

/**
 * Implements hook_field_schema().
 */
function field_nif_field_schema($field) {
  if ($field['type'] == 'nif') {
    return array(
      'columns' => array(
        'number' => array(
          'description' => 'Numeric part of the NIF',
          'type' => 'varchar',
          'length' => 8,
        ),
        'first_letter' => array(
          'description' => 'First letter of the NIF/CIF/NIE',
          'type' => 'varchar',
          'length' => 1,
        ),
        'last_letter' => array(
          'description' => 'Last letter of the NIF/CIF/NIE',
          'type' => 'varchar',
          'length' => 1,
        ),
        'type' => array(
          'description' => 'Type of the number, NIF/CIF/NIE',
          'type' => 'varchar',
          'length' => 10,
        ),
      ),
      'indexes' => array(),
    );
  }
}

/**
 * Implements hook_update_N().
 * Update the schema of all nif fields.
 */
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();
}

Functions

Namesort descending Description
field_nif_field_schema Implements hook_field_schema().
field_nif_update_7100 Implements hook_update_N(). Update the schema of all nif fields.