function hook_field_storage_config_update_forbid

Same name in other branches
  1. 9 core/modules/field/field.api.php \hook_field_storage_config_update_forbid()
  2. 8.9.x core/modules/field/field.api.php \hook_field_storage_config_update_forbid()
  3. 10 core/modules/field/field.api.php \hook_field_storage_config_update_forbid()

Forbid a field storage update from occurring.

Any module may forbid any update for any reason. For example, the field's storage module might forbid an update if it would change the storage schema while data for the field exists. A field type module might forbid an update if it would change existing data's semantics, or if there are external dependencies on field settings that cannot be updated.

To forbid the update from occurring, throw a \Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException.

Parameters

\Drupal\field\FieldStorageConfigInterface $field_storage: The field storage as it will be post-update.

\Drupal\field\FieldStorageConfigInterface $prior_field_storage: The field storage as it is pre-update.

See also

Entity CRUD, editing, and view hooks

Related topics

1 invocation of hook_field_storage_config_update_forbid()
FieldStorageConfig::preSaveUpdated in core/modules/field/src/Entity/FieldStorageConfig.php
Prepares saving an updated field definition.

File

core/modules/field/field.api.php, line 137

Code

function hook_field_storage_config_update_forbid(FieldStorageConfigInterface $field_storage, FieldStorageConfigInterface $prior_field_storage) : void {
    if ($field_storage->getTypeProvider() == 'options' && $field_storage->hasData()) {
        // Forbid any update that removes allowed values with actual data.
        $allowed_values = $field_storage->getSetting('allowed_values');
        $prior_allowed_values = $prior_field_storage->getSetting('allowed_values');
        $lost_keys = array_keys(array_diff_key($prior_allowed_values, $allowed_values));
        if (_options_values_in_use($field_storage->getTargetEntityTypeId(), $field_storage->getName(), $lost_keys)) {
            throw new FieldStorageDefinitionUpdateForbiddenException("A list field '{$field_storage->getName()}' with existing data cannot have its keys changed.");
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.