View source
<?php
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(),
);
}
}
function field_nif_update_7100(&$sandbox) {
$fields = field_info_fields();
foreach ($fields as $field_name => $field) {
if ($field['type'] == 'nif') {
$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,
);
$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();
db_rename_table('field_data_' . $field_name, 'field_data_' . $field_name . '_old');
db_rename_table('field_revision_' . $field_name, 'field_revision_' . $field_name . '_old');
db_create_table('field_data_' . $field_name, $schema['field_data_' . $field_name]);
db_create_table('field_revision_' . $field_name, $schema['field_revision_' . $field_name]);
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();
}
db_drop_table('field_data_' . $field_name . '_old');
db_drop_table('field_revision_' . $field_name . '_old');
}
}
field_cache_clear();
}