Skip to content

Instantly share code, notes, and snippets.

@kporras07
Created July 21, 2020 13:16
Show Gist options
  • Save kporras07/68bd44f8b7210a5cc06e277e072e2de9 to your computer and use it in GitHub Desktop.
Save kporras07/68bd44f8b7210a5cc06e277e072e2de9 to your computer and use it in GitHub Desktop.
Resize field
use Drupal\field\Entity\FieldStorageConfig;
/**
* Resize field.
*/
function demo_field_resize_update_8001() {
$field_name = 'field_to_resize';
$entity_type = 'node';
$database = \Drupal::database();
$database->query("ALTER TABLE node__" . $field_name . " MODIFY " . $field_name . "_value VARCHAR(200)");
$database->query("ALTER TABLE node_revision__" . $field_name . " MODIFY " . $field_name . "_value VARCHAR(200)");
$storage_key = 'node.field_schema_data.' . $field_name;
$storage_schema = \Drupal::keyValue('entity.storage_schema.sql');
$field_schema = $storage_schema->get($storage_key);
$field_schema['node__' . $field_name]['fields'][$field_name . '_value']['length'] = 200;
$field_schema['node_revision__' . $field_name]['fields'][$field_name . '_value']['length'] = 200;
$storage_schema->set($storage_key, $field_schema);
$config = \Drupal::configFactory()
->getEditable('field.storage.node.' . $field_name);
$config->set('settings.max_length', 200);
$config->save(TRUE);
FieldStorageConfig::loadByName($entity_type, $field_name)->save();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment