entityTypeManager = $entity_type_manager; $this->eventDispatcher = $event_dispatcher; $this->entityLastInstalledSchemaRepository = $entity_last_installed_schema_repository; $this->entityFieldManager = $entity_field_manager; } /** * {@inheritdoc} */ public function onFieldStorageDefinitionCreate(FieldStorageDefinitionInterface $storage_definition) { $entity_type_id = $storage_definition->getTargetEntityTypeId(); // @todo Forward this to all interested handlers, not only storage, once // iterating handlers is possible: https://www.drupal.org/node/2332857. $storage = clone $this->entityTypeManager->getStorage($entity_type_id); // Entity type definition updates can change the schema by adding or // removing entity tables (for example when switching an entity type from // non-revisionable to revisionable), so CRUD operations on a field storage // definition need to use the last installed entity type schema. if ($storage instanceof SqlContentEntityStorage && ($last_installed_entity_type = $this->entityLastInstalledSchemaRepository->getLastInstalledDefinition($entity_type_id))) { $storage->setEntityType($last_installed_entity_type); } if ($storage instanceof FieldStorageDefinitionListenerInterface) { $storage->onFieldStorageDefinitionCreate($storage_definition); } $this->eventDispatcher->dispatch(FieldStorageDefinitionEvents::CREATE, new FieldStorageDefinitionEvent($storage_definition)); $this->entityLastInstalledSchemaRepository->setLastInstalledFieldStorageDefinition($storage_definition); $this->entityFieldManager->clearCachedFieldDefinitions(); } /** * {@inheritdoc} */ public function onFieldStorageDefinitionUpdate(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) { $entity_type_id = $storage_definition->getTargetEntityTypeId(); // @todo Forward this to all interested handlers, not only storage, once // iterating handlers is possible: https://www.drupal.org/node/2332857. $storage = clone $this->entityTypeManager->getStorage($entity_type_id); // Entity type definition updates can change the schema by adding or // removing entity tables (for example when switching an entity type from // non-revisionable to revisionable), so CRUD operations on a field storage // definition need to use the last installed entity type schema. if ($storage instanceof SqlContentEntityStorage && ($last_installed_entity_type = $this->entityLastInstalledSchemaRepository->getLastInstalledDefinition($entity_type_id))) { $storage->setEntityType($last_installed_entity_type); } if ($storage instanceof FieldStorageDefinitionListenerInterface) { $storage->onFieldStorageDefinitionUpdate($storage_definition, $original); } $this->eventDispatcher->dispatch(FieldStorageDefinitionEvents::UPDATE, new FieldStorageDefinitionEvent($storage_definition, $original)); $this->entityLastInstalledSchemaRepository->setLastInstalledFieldStorageDefinition($storage_definition); $this->entityFieldManager->clearCachedFieldDefinitions(); } /** * {@inheritdoc} */ public function onFieldStorageDefinitionDelete(FieldStorageDefinitionInterface $storage_definition) { $entity_type_id = $storage_definition->getTargetEntityTypeId(); // @todo Forward this to all interested handlers, not only storage, once // iterating handlers is possible: https://www.drupal.org/node/2332857. $storage = clone $this->entityTypeManager->getStorage($entity_type_id); // Entity type definition updates can change the schema by adding or // removing entity tables (for example when switching an entity type from // non-revisionable to revisionable), so CRUD operations on a field storage // definition need to use the last installed entity type schema. if ($storage instanceof SqlContentEntityStorage && ($last_installed_entity_type = $this->entityLastInstalledSchemaRepository->getLastInstalledDefinition($entity_type_id))) { $storage->setEntityType($last_installed_entity_type); } if ($storage instanceof FieldStorageDefinitionListenerInterface) { $storage->onFieldStorageDefinitionDelete($storage_definition); } $this->eventDispatcher->dispatch(FieldStorageDefinitionEvents::DELETE, new FieldStorageDefinitionEvent($storage_definition)); $this->entityLastInstalledSchemaRepository->deleteLastInstalledFieldStorageDefinition($storage_definition); $this->entityFieldManager->clearCachedFieldDefinitions(); } }