* {@inheritdoc}
*/
public static function preDelete(EntityStorageInterface $storage, array $fields) {
- $state = \Drupal::state();
+ /** @var \Drupal\Core\Field\DeletedFieldsRepositoryInterface $deleted_fields_repository */
+ $deleted_fields_repository = \Drupal::service('entity_field.deleted_fields_repository');
$entity_type_manager = \Drupal::entityTypeManager();
parent::preDelete($storage, $fields);
- // Keep the field definitions in the state storage so we can use them
- // later during field_purge_batch().
- $deleted_fields = $state->get('field.field.deleted') ?: [];
+ // Keep the field definitions in the deleted fields repository so we can use
+ // them later during field_purge_batch().
/** @var \Drupal\field\FieldConfigInterface $field */
foreach ($fields as $field) {
// Only mark a field for purging if there is data. Otherwise, just remove
// it.
$target_entity_storage = $entity_type_manager->getStorage($field->getTargetEntityTypeId());
if (!$field->deleted && $target_entity_storage instanceof FieldableEntityStorageInterface && $target_entity_storage->countFieldData($field->getFieldStorageDefinition(), TRUE)) {
- $config = $field->toArray();
- $config['deleted'] = TRUE;
- $config['field_storage_uuid'] = $field->getFieldStorageDefinition()->uuid();
- $deleted_fields[$field->uuid()] = $config;
+ $field = clone $field;
+ $field->deleted = TRUE;
+ $field->fieldStorage = NULL;
+ $deleted_fields_repository->addFieldDefinition($field);
}
}
- $state->set('field.field.deleted', $deleted_fields);
}
/**
*/
public function getFieldStorageDefinition() {
if (!$this->fieldStorage) {
- $fields = $this->entityManager()->getFieldStorageDefinitions($this->entity_type);
- if (!isset($fields[$this->field_name])) {
+ $field_storage_definition = NULL;
+
+ $field_storage_definitions = $this->entityManager()->getFieldStorageDefinitions($this->entity_type);
+ if (isset($field_storage_definitions[$this->field_name])) {
+ $field_storage_definition = $field_storage_definitions[$this->field_name];
+ }
+ // If this field has been deleted, try to find its field storage
+ // definition in the deleted fields repository.
+ elseif ($this->deleted) {
+ $deleted_storage_definitions = \Drupal::service('entity_field.deleted_fields_repository')->getFieldStorageDefinitions();
+ foreach ($deleted_storage_definitions as $deleted_storage_definition) {
+ if ($deleted_storage_definition->getName() === $this->field_name) {
+ $field_storage_definition = $deleted_storage_definition;
+ }
+ }
+ }
+
+ if (!$field_storage_definition) {
throw new FieldException("Attempt to create a field {$this->field_name} that does not exist on entity type {$this->entity_type}.");
}
- if (!$fields[$this->field_name] instanceof FieldStorageConfigInterface) {
+ if (!$field_storage_definition instanceof FieldStorageConfigInterface) {
throw new FieldException("Attempt to create a configurable field of non-configurable field storage {$this->field_name}.");
}
- $this->fieldStorage = $fields[$this->field_name];
+ $this->fieldStorage = $field_storage_definition;
}
return $this->fieldStorage;
return FALSE;
}
+ /**
+ * {@inheritdoc}
+ */
+ public function getUniqueIdentifier() {
+ return $this->uuid();
+ }
+
/**
* Loads a field config entity based on the entity type and field name.
*