Last active
October 11, 2022 14:16
-
-
Save mecmartini/cce8bd3808502c08ac66d1afecfca8f2 to your computer and use it in GitHub Desktop.
Drupal - Resize field with already existing data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Increase the length of "field_one" to 512 characters. | |
*/ | |
function trimis_cordis_update_9001() { | |
// Resize field DB column | |
$database = \Drupal::database(); | |
$database->query("ALTER TABLE node__field_one MODIFY field_one_value VARCHAR(512)"); | |
$database->query("ALTER TABLE node_revision__field_one MODIFY field_one_value VARCHAR(512)"); | |
// Update field schema | |
$storage_key = 'node.field_schema_data.field_one'; | |
$storage_schema = \Drupal::keyValue('entity.storage_schema.sql'); | |
$field_schema = $storage_schema->get($storage_key); | |
$field_schema['node__field_one']['fields']['field_one_value']['length'] = 512; | |
$field_schema['node_revision__field_one']['fields']['field_one_value']['length'] = 512; | |
$storage_schema->set($storage_key, $field_schema); | |
// Update field configuration | |
$config = \Drupal::configFactory() | |
->getEditable('field.storage.node.field_one'); | |
$config->set('settings.max_length', 512); | |
$config->save(TRUE); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment