hook_field_storage_config_update_forbid

hook_field_storage_config_update_forbid(\Drupal\field\FieldStorageConfigInterface $field_storage, \Drupal\field\FieldStorageConfigInterface $prior_field_storage)

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

Field Types API
Defines field, widget, display formatter, and storage types.
Hooks
Define functions that alter the behavior of Drupal core.

File

core/modules/field/field.api.php, line 81
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->module == '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(t('A list field (@field_name) with existing data cannot have its keys changed.', array('@field_name' => $field_storage->getName())));
    }
  }
}
doc_Drupal
2016-10-29 09:18:10
Comments
Leave a Comment

Please login to continue.