Mercurial > hg > isophonics-drupal-site
comparison core/modules/field/src/Entity/FieldConfig.php @ 14:1fec387a4317
Update Drupal core to 8.5.2 via Composer
| author | Chris Cannam |
|---|---|
| date | Mon, 23 Apr 2018 09:46:53 +0100 |
| parents | 4c8ae668cc8c |
| children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
| 13:5fb285c0d0e3 | 14:1fec387a4317 |
|---|---|
| 187 | 187 |
| 188 /** | 188 /** |
| 189 * {@inheritdoc} | 189 * {@inheritdoc} |
| 190 */ | 190 */ |
| 191 public static function preDelete(EntityStorageInterface $storage, array $fields) { | 191 public static function preDelete(EntityStorageInterface $storage, array $fields) { |
| 192 $state = \Drupal::state(); | 192 /** @var \Drupal\Core\Field\DeletedFieldsRepositoryInterface $deleted_fields_repository */ |
| 193 $deleted_fields_repository = \Drupal::service('entity_field.deleted_fields_repository'); | |
| 193 $entity_type_manager = \Drupal::entityTypeManager(); | 194 $entity_type_manager = \Drupal::entityTypeManager(); |
| 194 | 195 |
| 195 parent::preDelete($storage, $fields); | 196 parent::preDelete($storage, $fields); |
| 196 // Keep the field definitions in the state storage so we can use them | 197 |
| 197 // later during field_purge_batch(). | 198 // Keep the field definitions in the deleted fields repository so we can use |
| 198 $deleted_fields = $state->get('field.field.deleted') ?: []; | 199 // them later during field_purge_batch(). |
| 199 | |
| 200 /** @var \Drupal\field\FieldConfigInterface $field */ | 200 /** @var \Drupal\field\FieldConfigInterface $field */ |
| 201 foreach ($fields as $field) { | 201 foreach ($fields as $field) { |
| 202 // Only mark a field for purging if there is data. Otherwise, just remove | 202 // Only mark a field for purging if there is data. Otherwise, just remove |
| 203 // it. | 203 // it. |
| 204 $target_entity_storage = $entity_type_manager->getStorage($field->getTargetEntityTypeId()); | 204 $target_entity_storage = $entity_type_manager->getStorage($field->getTargetEntityTypeId()); |
| 205 if (!$field->deleted && $target_entity_storage instanceof FieldableEntityStorageInterface && $target_entity_storage->countFieldData($field->getFieldStorageDefinition(), TRUE)) { | 205 if (!$field->deleted && $target_entity_storage instanceof FieldableEntityStorageInterface && $target_entity_storage->countFieldData($field->getFieldStorageDefinition(), TRUE)) { |
| 206 $config = $field->toArray(); | 206 $field = clone $field; |
| 207 $config['deleted'] = TRUE; | 207 $field->deleted = TRUE; |
| 208 $config['field_storage_uuid'] = $field->getFieldStorageDefinition()->uuid(); | 208 $field->fieldStorage = NULL; |
| 209 $deleted_fields[$field->uuid()] = $config; | 209 $deleted_fields_repository->addFieldDefinition($field); |
| 210 } | 210 } |
| 211 } | 211 } |
| 212 $state->set('field.field.deleted', $deleted_fields); | |
| 213 } | 212 } |
| 214 | 213 |
| 215 /** | 214 /** |
| 216 * {@inheritdoc} | 215 * {@inheritdoc} |
| 217 */ | 216 */ |
| 286 /** | 285 /** |
| 287 * {@inheritdoc} | 286 * {@inheritdoc} |
| 288 */ | 287 */ |
| 289 public function getFieldStorageDefinition() { | 288 public function getFieldStorageDefinition() { |
| 290 if (!$this->fieldStorage) { | 289 if (!$this->fieldStorage) { |
| 291 $fields = $this->entityManager()->getFieldStorageDefinitions($this->entity_type); | 290 $field_storage_definition = NULL; |
| 292 if (!isset($fields[$this->field_name])) { | 291 |
| 292 $field_storage_definitions = $this->entityManager()->getFieldStorageDefinitions($this->entity_type); | |
| 293 if (isset($field_storage_definitions[$this->field_name])) { | |
| 294 $field_storage_definition = $field_storage_definitions[$this->field_name]; | |
| 295 } | |
| 296 // If this field has been deleted, try to find its field storage | |
| 297 // definition in the deleted fields repository. | |
| 298 elseif ($this->deleted) { | |
| 299 $deleted_storage_definitions = \Drupal::service('entity_field.deleted_fields_repository')->getFieldStorageDefinitions(); | |
| 300 foreach ($deleted_storage_definitions as $deleted_storage_definition) { | |
| 301 if ($deleted_storage_definition->getName() === $this->field_name) { | |
| 302 $field_storage_definition = $deleted_storage_definition; | |
| 303 } | |
| 304 } | |
| 305 } | |
| 306 | |
| 307 if (!$field_storage_definition) { | |
| 293 throw new FieldException("Attempt to create a field {$this->field_name} that does not exist on entity type {$this->entity_type}."); | 308 throw new FieldException("Attempt to create a field {$this->field_name} that does not exist on entity type {$this->entity_type}."); |
| 294 } | 309 } |
| 295 if (!$fields[$this->field_name] instanceof FieldStorageConfigInterface) { | 310 if (!$field_storage_definition instanceof FieldStorageConfigInterface) { |
| 296 throw new FieldException("Attempt to create a configurable field of non-configurable field storage {$this->field_name}."); | 311 throw new FieldException("Attempt to create a configurable field of non-configurable field storage {$this->field_name}."); |
| 297 } | 312 } |
| 298 $this->fieldStorage = $fields[$this->field_name]; | 313 $this->fieldStorage = $field_storage_definition; |
| 299 } | 314 } |
| 300 | 315 |
| 301 return $this->fieldStorage; | 316 return $this->fieldStorage; |
| 302 } | 317 } |
| 303 | 318 |
| 326 /** | 341 /** |
| 327 * {@inheritdoc} | 342 * {@inheritdoc} |
| 328 */ | 343 */ |
| 329 public function isComputed() { | 344 public function isComputed() { |
| 330 return FALSE; | 345 return FALSE; |
| 346 } | |
| 347 | |
| 348 /** | |
| 349 * {@inheritdoc} | |
| 350 */ | |
| 351 public function getUniqueIdentifier() { | |
| 352 return $this->uuid(); | |
| 331 } | 353 } |
| 332 | 354 |
| 333 /** | 355 /** |
| 334 * Loads a field config entity based on the entity type and field name. | 356 * Loads a field config entity based on the entity type and field name. |
| 335 * | 357 * |
