Update max length for the existing text field
Posted by edsardachuk - July 17, 2025
You may need to do this if you didn't use text long when creating a field and are now building a custom migration, and some source values are too long for the field you created🙄
If you need to do it in a live environment, use the following code snippet to create a custom Drush command or an update hook. In my case, I just run it as a script locally because the situation allowed it.
$database = \Drupal::database();
$database->query("ALTER TABLE node__field_plain_text MODIFY field_plain_text_value VARCHAR(1000)");
$database->query("ALTER TABLE node_revision__field_plain_text MODIFY field_plain_text_value VARCHAR(1000)");
$storage_key = 'node.field_schema_data.field_target_zipcode';
$storage_schema = \Drupal::keyValue('entity.storage_schema.sql');
$field_schema = $storage_schema->get($storage_key);
$field_schema['node__field_plain_text']['fields']['field_plain_text_value']['length'] = 1000;
$field_schema['node_revision__field_plain_text']['fields']['field_plain_text_value']['length'] = 1000;
$storage_schema->set($storage_key, $field_schema);
$config = \Drupal::configFactory()
->getEditable('field.storage.node.field_plain_text');
$config->set('settings.max_length', 1000);
$config->save(TRUE);
FieldStorageConfig::loadByName('node', 'field_plain_text')->save();