diff --git a/src/EntityUsage.php b/src/EntityUsage.php index 3d0beff..403bd29 100644 --- a/src/EntityUsage.php +++ b/src/EntityUsage.php @@ -105,7 +105,16 @@ class EntityUsage implements EntityUsageInterface { // If $count is 0, we want to delete the record. if ($count <= 0) { - $this->deleteRecord($target_id, $target_type, $source_id, $source_type, $source_langcode, $source_vid, $method, $field_name); + $this->connection->delete($this->tableName) + ->condition('target_id', $target_id) + ->condition('target_type', $target_type) + ->condition('source_id', $source_id) + ->condition('source_type', $source_type) + ->condition('source_langcode', $source_langcode) + ->condition('source_vid', $source_vid) + ->condition('method', $method) + ->condition('field_name', $field_name) + ->execute(); } else { $this->connection->merge($this->tableName) @@ -130,22 +139,6 @@ class EntityUsage implements EntityUsageInterface { /** * {@inheritdoc} */ - public function deleteRecord($target_id, $target_type, $source_id, $source_type, $source_langcode, $source_vid, $method, $field_name) { - $this->connection->delete($this->tableName) - ->condition('target_id', $target_id) - ->condition('target_type', $target_type) - ->condition('source_id', $source_id) - ->condition('source_type', $source_type) - ->condition('source_langcode', $source_langcode) - ->condition('source_vid', $source_vid) - ->condition('method', $method) - ->condition('field_name', $field_name) - ->execute(); - } - - /** - * {@inheritdoc} - */ public function bulkDeleteTargets($target_type) { $query = $this->connection->delete($this->tableName) ->condition('target_type', $target_type); diff --git a/src/EntityUsageInterface.php b/src/EntityUsageInterface.php index 7eb89ef..c45cfdf 100644 --- a/src/EntityUsageInterface.php +++ b/src/EntityUsageInterface.php @@ -45,29 +45,6 @@ interface EntityUsageInterface { public function registerUsage($target_id, $target_type, $source_id, $source_type, $source_langcode, $source_vid, $method, $field_name, $count = 1); /** - * Delete a single usage record. - * - * @param int $target_id - * The target entity ID. - * @param string $target_type - * The target entity type. - * @param int $source_id - * The source entity ID. - * @param string $source_type - * The source entity type. - * @param string $source_langcode - * The source entity language code. - * @param string $source_vid - * The source entity revision ID. - * @param string $method - * The method used to relate source entity with the target entity. Normally - * the plugin id. - * @param string $field_name - * The name of the field in the source entity using the target entity. - */ - public function deleteRecord($target_id, $target_type, $source_id, $source_type, $source_langcode, $source_vid, $method, $field_name); - - /** * Remove all records of a given target entity type. * * @param string $target_type diff --git a/src/Plugin/EntityUsage/Track/EntityReference.php b/src/Plugin/EntityUsage/Track/EntityReference.php index 5113a2e..ea496d0 100644 --- a/src/Plugin/EntityUsage/Track/EntityReference.php +++ b/src/Plugin/EntityUsage/Track/EntityReference.php @@ -187,7 +187,7 @@ class EntityReference extends EntityUsageTrackBase { /** @var \Drupal\field\Entity\FieldConfig $definition */ $definition = $this->entityFieldManager->getFieldDefinitions($source_entity->getEntityTypeId(), $source_entity->bundle())[$field_name]; $target_type = $definition->getSetting('target_type'); - $this->usageService->deleteRecord($target_id, $target_type, $source_entity->id(), $source_entity->getEntityTypeId(), $source_entity->language()->getId(), $source_entity->getRevisionId(), $this->pluginId, $field_name); + $this->usageService->registerUsage($target_id, $target_type, $source_entity->id(), $source_entity->getEntityTypeId(), $source_entity->language()->getId(), $source_entity->getRevisionId(), $this->pluginId, $field_name, 0); } } diff --git a/src/Plugin/EntityUsage/Track/Link.php b/src/Plugin/EntityUsage/Track/Link.php index a3f0eb4..380c0cd 100644 --- a/src/Plugin/EntityUsage/Track/Link.php +++ b/src/Plugin/EntityUsage/Track/Link.php @@ -149,7 +149,7 @@ class Link extends EntityUsageTrackBase { } foreach ($removed_ids as $removed_entity) { list($target_type, $target_id) = explode('|', $removed_entity); - $this->usageService->deleteRecord($target_id, $target_type, $source_entity->id(), $source_entity->getEntityTypeId(), $source_entity->language()->getId(), $source_entity->getRevisionId(), $this->pluginId, $field_name); + $this->usageService->registerUsage($target_id, $target_type, $source_entity->id(), $source_entity->getEntityTypeId(), $source_entity->language()->getId(), $source_entity->getRevisionId(), $this->pluginId, $field_name, 0); } } } diff --git a/src/Plugin/EntityUsage/Track/TextFieldEmbedBase.php b/src/Plugin/EntityUsage/Track/TextFieldEmbedBase.php index 9b1228f..54266f6 100644 --- a/src/Plugin/EntityUsage/Track/TextFieldEmbedBase.php +++ b/src/Plugin/EntityUsage/Track/TextFieldEmbedBase.php @@ -190,10 +190,7 @@ abstract class TextFieldEmbedBase extends EntityUsageTrackBase implements EmbedT protected function decrementEmbeddedUsage(ContentEntityInterface $source_entity, $target_type, $uuid, $field_name) { /** @var \Drupal\Core\Entity\ContentEntityInterface $target_entity */ $target_entity = $this->entityRepository->loadEntityByUuid($target_type, $uuid); - // @todo Fix this when multiple usage tracking in the same field to the same - // entity is supported. When that is done, this should just decrement the - // count by 1, instead of removing the record entirely. - $this->usageService->deleteRecord($target_entity->id(), $target_type, $source_entity->id(), $source_entity->getEntityTypeId(), $source_entity->language()->getId(), $source_entity->getRevisionId(), $this->pluginId, $field_name); + $this->usageService->registerUsage($target_entity->id(), $target_type, $source_entity->id(), $source_entity->getEntityTypeId(), $source_entity->language()->getId(), $source_entity->getRevisionId(), $this->pluginId, $field_name, 0); } } diff --git a/tests/src/Kernel/EntityUsageTest.php b/tests/src/Kernel/EntityUsageTest.php index f54506a..28bcbf3 100644 --- a/tests/src/Kernel/EntityUsageTest.php +++ b/tests/src/Kernel/EntityUsageTest.php @@ -190,7 +190,7 @@ class EntityUsageTest extends EntityKernelTestBase { $this->assertEquals(1, $real_usage); // Track an usage down. - $entity_usage->deleteRecord($entity->id(), $entity->getEntityTypeId(), 1, 'foo', 'en', 1, 'entity_reference', $field_name); + $entity_usage->registerUsage($entity->id(), $entity->getEntityTypeId(), 1, 'foo', 'en', 1, 'entity_reference', $field_name, 0); $real_usage = $this->injectedDatabase->select($this->tableName, 'e') ->fields('e', ['count'])