Commit 717243cc authored by alexpott's avatar alexpott

Issue #2296787 by gauravkhambhala | jhodgdon: Fixed...

Issue #2296787 by gauravkhambhala | jhodgdon: Fixed hook_field_config_update_forbid() is in the wrong group/topic.
parent 945ed201
......@@ -55,6 +55,46 @@ function hook_field_info_alter(&$info) {
}
}
/**
* 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.
*
* @param \Drupal\field\FieldStorageConfigInterface $field_storage
* The field storage as it will be post-update.
* @param \Drupal\field\FieldStorageConfigInterface $prior_field_storage
* The field storage as it is pre-update.
*
* @see entity_crud
*/
function hook_field_storage_config_update_forbid(\Drupal\field\FieldStorageConfigInterface $field_storage, \Drupal\field\FieldStorageConfigInterface $prior_field_storage) {
// A 'list' field stores integer keys mapped to display values. If
// the new field will have fewer values, and any data exists for the
// abandoned keys, the field will have no way to display them. So,
// forbid such an update.
if ($field_storage->hasData() && count($field_storage['settings']['allowed_values']) < count($prior_field_storage['settings']['allowed_values'])) {
// Identify the keys that will be lost.
$lost_keys = array_diff(array_keys($field_storage['settings']['allowed_values']), array_keys($prior_field_storage['settings']['allowed_values']));
// If any data exist for those keys, forbid the update.
$query = new EntityFieldQuery();
$found = $query
->fieldCondition($prior_field_storage['field_name'], 'value', $lost_keys)
->range(0, 1)
->execute();
if ($found) {
throw new \Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException("Cannot update a list field storage not to include keys with existing data");
}
}
}
/**
* @} End of "defgroup field_types".
*/
......@@ -236,46 +276,6 @@ function hook_field_info_max_weight($entity_type, $bundle, $context, $context_mo
* @{
*/
/**
* 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.
*
* @param \Drupal\field\FieldStorageConfigInterface $field_storage
* The field storage as it will be post-update.
* @param \Drupal\field\FieldStorageConfigInterface $prior_field_storage
* The field storage as it is pre-update.
*
* @see entity_crud
*/
function hook_field_storage_config_update_forbid(\Drupal\field\FieldStorageConfigInterface $field_storage, \Drupal\field\FieldStorageConfigInterface $prior_field_storage) {
// A 'list' field stores integer keys mapped to display values. If
// the new field will have fewer values, and any data exists for the
// abandoned keys, the field will have no way to display them. So,
// forbid such an update.
if ($field_storage->hasData() && count($field_storage['settings']['allowed_values']) < count($prior_field_storage['settings']['allowed_values'])) {
// Identify the keys that will be lost.
$lost_keys = array_diff(array_keys($field_storage['settings']['allowed_values']), array_keys($prior_field_storage['settings']['allowed_values']));
// If any data exist for those keys, forbid the update.
$query = new EntityFieldQuery();
$found = $query
->fieldCondition($prior_field_storage['field_name'], 'value', $lost_keys)
->range(0, 1)
->execute();
if ($found) {
throw new \Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException("Cannot update a list field storage not to include keys with existing data");
}
}
}
/**
* Acts when a field storage definition is being purged.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment