Same name and namespace in other branches
  1. 8.9.x core/modules/field/field.api.php \hook_field_storage_config_update_forbid()
  2. 9 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

2 functions implement hook_field_storage_config_update_forbid()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

field_test_field_storage_config_update_forbid in core/modules/field/tests/modules/field_test/field_test.field.inc
Implements hook_field_storage_config_update_forbid().
options_field_storage_config_update_forbid in core/modules/options/options.module
Implements hook_field_storage_config_update_forbid().

File

core/modules/field/field.api.php, line 127
Field API documentation.

Code

function hook_field_storage_config_update_forbid(\Drupal\field\FieldStorageConfigInterface $field_storage, \Drupal\field\FieldStorageConfigInterface $prior_field_storage) {
  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 \Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException("A list field '{$field_storage->getName()}' with existing data cannot have its keys changed.");
    }
  }
}