4

I have a textfield that is part of a field-collection belonging to a content type made in a custom module. I am looking for a way to expand the size of the field on the update hook of my module.

I've tried the following:

function spelersfiche_update_7200() {
  field_update_field(array(
    'field_name' => 'spelersfiche_va_antwoord',
    'cardinality' => '1',
    'type' => 'text',
    'settings' => array(
      'max_length' => '511',
    )
  ));
}

In this the max_length setting has been increased. When i try to run the update for my module i get the following error:

The following updates returned messages
spelersfiche module
Update #7200

Failed: FieldUpdateForbiddenException: field_sql_storage cannot change the schema for an existing field with data. in field_sql_storage_field_update_forbid() (line 229 of D:\drupal\volley3\modules\field\modules\field_sql_storage\field_sql_storage.module).

From what i understand it says i can't change this setting for a field that contains data. But in my case it's just to make the field larger, so any previously contained data should still be able to fit into the field.

Is there any way around this, because uninstalling and reinstalling is what i was trying to avoid since that will delete all the data contained in the field.

8

If you want to get around the restrictions imposed by the FieldAPI, and you're positive the change won't cause any data loss, you could just do it manually:

$field_name = 'spelersfiche_va_antwoord';

// Get the current settings
$result = db_query('SELECT data FROM {field_config} WHERE field_name = :name', array(':name' => $field_name))->fetchField();

// Change the settings
$data = unserialize($result);
$data['settings']['max_length'] = '511';

// Write settings back to the database.
db_update('field_config')
  ->fields(array('data' => serialize($data)))
  ->condition('field_name', $field_name)
  ->execute();

// Update the value column in both the _data and _revision tables for the field
$new_field = array('type' => 'varchar', 'length' => 511);
$col_name = $field_name . '_value';

db_change_field("field_data_$field_name", $col_name, $col_name, $new_field);
db_change_field("field_revision_$field_name", $col_name, $col_name, $new_field);

// Flush the caches
drupal_flush_all_caches();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.