Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / lib / Drupal / Core / Field / FieldStorageDefinitionListener.php
index 154e485ecbd5a6cd8be9912891eff1f161af09aa..e67629b75932beb6e2829d3361585231995ed7df 100644 (file)
@@ -2,9 +2,11 @@
 
 namespace Drupal\Core\Field;
 
+use Drupal\Core\Database\DatabaseExceptionWrapper;
 use Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface;
 use Drupal\Core\Entity\EntityFieldManagerInterface;
 use Drupal\Core\Entity\EntityTypeManagerInterface;
+use Drupal\Core\Entity\FieldableEntityStorageInterface;
 use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 
@@ -43,6 +45,13 @@ class FieldStorageDefinitionListener implements FieldStorageDefinitionListenerIn
    */
   protected $entityFieldManager;
 
+  /**
+   * The deleted fields repository.
+   *
+   * @var \Drupal\Core\Field\DeletedFieldsRepositoryInterface
+   */
+  protected $deletedFieldsRepository;
+
   /**
    * Constructs a new FieldStorageDefinitionListener.
    *
@@ -54,12 +63,15 @@ class FieldStorageDefinitionListener implements FieldStorageDefinitionListenerIn
    *   The entity last installed schema repository.
    * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
    *   The entity field manager.
+   * @param \Drupal\Core\Field\DeletedFieldsRepositoryInterface $deleted_fields_repository
+   *   The deleted fields repository.
    */
-  public function __construct(EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository, EntityFieldManagerInterface $entity_field_manager) {
+  public function __construct(EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository, EntityFieldManagerInterface $entity_field_manager, DeletedFieldsRepositoryInterface $deleted_fields_repository) {
     $this->entityTypeManager = $entity_type_manager;
     $this->eventDispatcher = $event_dispatcher;
     $this->entityLastInstalledSchemaRepository = $entity_last_installed_schema_repository;
     $this->entityFieldManager = $entity_field_manager;
+    $this->deletedFieldsRepository = $deleted_fields_repository;
   }
 
   /**
@@ -139,6 +151,23 @@ class FieldStorageDefinitionListener implements FieldStorageDefinitionListenerIn
       $storage->setEntityType($last_installed_entity_type);
     }
 
+    // Keep the field definition in the deleted fields repository so we can use
+    // it later during field_purge_batch(), but only if the field has data.
+    try {
+      if ($storage_definition instanceof BaseFieldDefinition && $storage instanceof FieldableEntityStorageInterface && $storage->countFieldData($storage_definition, TRUE)) {
+        $deleted_storage_definition = clone $storage_definition;
+        $deleted_storage_definition->setDeleted(TRUE);
+        $this->deletedFieldsRepository->addFieldDefinition($deleted_storage_definition);
+        $this->deletedFieldsRepository->addFieldStorageDefinition($deleted_storage_definition);
+      }
+    }
+    catch (DatabaseExceptionWrapper $e) {
+      // This may happen when changing field storage schema, since we are not
+      // able to use a table mapping matching the passed storage definition.
+      // @todo Revisit this once we are able to instantiate the table mapping
+      //   properly. See https://www.drupal.org/node/2274017.
+    }
+
     if ($storage instanceof FieldStorageDefinitionListenerInterface) {
       $storage->onFieldStorageDefinitionDelete($storage_definition);
     }