My module provides config that creates a field:
field.storage.paragraph.field_accordion_section_title.yml
langcode: en
status: true
dependencies:
module:
- paragraphs
- text
id: paragraph.field_accordion_section_title
field_name: field_accordion_section_title
entity_type: paragraph
type: text_long
settings: { }
module: text
locked: false
cardinality: 1
translatable: true
indexes: { }
persist_with_no_fields: false
custom_storage: false
I would like to update the field to use string_long
instead of text_long
. I would like to handle this via hook_update() since the module was already installed on multiple sites. I tried the following:
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
/**
* Updates accordion section title to string type.
*/
function my_module_update_8001() {
if ($fields = \Drupal::entityTypeManager()->getStorage('field_storage_config')->loadByProperties(array('field_name' => 'field_accordion_section_title'))) {
foreach ($fields as $field) {
$new_field = $field->toArray();
$new_field['type'] = 'string_long';
$new_field = FieldStorageConfig::create($new_field);
$new_field->original = $field;
$new_field->enforceIsNew(FALSE);
$new_field->save();
}
}
}
I get the following error when updating: Failed: Cannot change the field type for an existing field storage.
enforceIsNew(FALSE)
, you are forcing Drupal not to consider it a new field. Did you try withenforceIsNew(TRUE)
? I would also avoid using$new_field->original = $field;
because that is telling Drupal that the field is being updated.