Last active
February 22, 2026 07:55
-
-
Save baikho/c04ea2342fa8df26b1e9db4191817c47 to your computer and use it in GitHub Desktop.
Drupal update hook to increase max_length on existing Paragraph text fields by updating schema and field storage config
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 max_length for specific paragraph text fields. | |
| */ | |
| function my_module_update_10001() { | |
| $fields = [ | |
| 'field_caption', | |
| ]; | |
| $new_max_length = 765; | |
| $database = \Drupal::database(); | |
| foreach ($fields as $field_name) { | |
| $storage_key = 'paragraph.' . $field_name; | |
| // Update the database schema first (before touching config). | |
| // Paragraph fields use dedicated data and revision tables. | |
| $data_table = 'paragraph__' . $field_name; | |
| $revision_table = 'paragraph_revision__' . $field_name; | |
| $column_name = $field_name . '_value'; | |
| foreach ([$data_table, $revision_table] as $table_name) { | |
| if ($database->schema()->tableExists($table_name)) { | |
| $database->schema()->changeField($table_name, $column_name, $column_name, [ | |
| 'type' => 'varchar', | |
| 'length' => $new_max_length, | |
| 'not null' => FALSE, | |
| ]); | |
| } | |
| } | |
| // Update the stored field storage config directly. | |
| // This avoids validation that blocks schema changes on fields with data. | |
| $config_name = 'field.storage.' . $storage_key; | |
| $config = \Drupal::configFactory()->getEditable($config_name); | |
| if (!$config->isNew()) { | |
| $config->set('settings.max_length', $new_max_length); | |
| $config->save(); | |
| } | |
| } | |
| // Clear cached field definitions and rebuild caches. | |
| \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions(); | |
| drupal_flush_all_caches(); | |
| return t('Updated max_length to @length for specified paragraph fields.', [ | |
| '@length' => $new_max_length, | |
| ]); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment