forked from evolvingweb/custom_field_resize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_field_resize.install
executable file
·48 lines (40 loc) · 1.75 KB
/
custom_field_resize.install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
/**
* @file
* Contains custom_field_resize.install.
*/
use Drupal\field\Entity\FieldStorageConfig;
/**
* Increase the length of "field_one_liner" to 255 characters.
*/
function custom_field_resize_update_8001() {
// Prepare relevant variables.
$entity_type = 'node';
$field_name = 'field_one_liner';
$field_length = 255;
// Update database schema.
$database = \Drupal::database();
// Resize the main field data table.
$database->query("ALTER TABLE {$entity_type}__{$field_name} MODIFY {$field_name}_value VARCHAR({$field_length})");
// Resize the revision field data table (if revisions are enabled).
$database->query("ALTER TABLE {$entity_type}_revision__{$field_name} MODIFY {$field_name}_value VARCHAR({$field_length})");
// Update storage schema.
$storage_key = $entity_type . '.field_schema_data.' . $field_name;
$storage_schema = \Drupal::keyValue('entity.storage_schema.sql');
$field_schema = $storage_schema->get($storage_key);
$field_schema[$entity_type . '__' . $field_name]['fields'][$field_name . '_value']['length'] = $field_length;
$field_schema[$entity_type . '_revision__' . $field_name]['fields'][$field_name . '_value']['length'] = $field_length;
$storage_schema->set($storage_key, $field_schema);
// Update field configuration.
$config = \Drupal::configFactory()
->getEditable("field.storage.{$entity_type}.{$field_name}");
$config->set('settings.max_length', $field_length);
$config->save(TRUE);
// Update field storage configuration.
FieldStorageConfig::loadByName($entity_type, $field_name)->save();
return t('Length of @entity-type.@field-name was updated to @field-length', [
'@entity-type' => $entity_type,
'@field-name' => $field_name,
'@field-length' => $field_length,
]);
}