diff --git a/src/Entity/Storage/FlaggingStorage.php b/src/Entity/Storage/FlaggingStorage.php index 6acc43b..8ed86aa 100644 --- a/src/Entity/Storage/FlaggingStorage.php +++ b/src/Entity/Storage/FlaggingStorage.php @@ -27,6 +27,15 @@ class FlaggingStorage extends SqlContentEntityStorage implements FlaggingStorage /** * {@inheritdoc} */ + public function resetCache(array $ids = NULL) { + parent::resetCache($ids); + $this->flagIdsByEntity = []; + $this->globalFlagIdsByEntity = []; + } + + /** + * {@inheritdoc} + */ public function loadIsFlagged(EntityInterface $entity, AccountInterface $account) { $flag_ids = $this->loadIsFlaggedMultiple([$entity], $account); return $flag_ids[$entity->id()]; @@ -113,5 +122,49 @@ class FlaggingStorage extends SqlContentEntityStorage implements FlaggingStorage return $flag_ids_by_entity; } + /** + * {@inheritdoc} + */ + protected function doPostSave(EntityInterface $entity, $update) { + + parent::doPostSave($entity, $update); + + // After updating or creating a flagging, add it to the cached flagging by entity if already in static cache. + if ($entity->get('global')->value) { + // If the global flags by entity for this entity have already been cached, then add the newly created flagging. + if (isset($this->globalFlagIdsByEntity[$entity->get('entity_type')->value][$entity->get('entity_id')->value])) { + $this->globalFlagIdsByEntity[$entity->get('entity_type')->value][$entity->get('entity_id')->value][$entity->get('flag_id')->value] = $entity->get('flag_id')->value; + } + } + else { + // If the flags by entity for this entity/user have already been cached, then add the newly created flagging. + if (isset($this->flagIdsByEntity[$entity->get('uid')->target_id][$entity->get('entity_type')->value][$entity->get('entity_id')->value])) { + $this->flagIdsByEntity[$entity->get('uid')->target_id][$entity->get('entity_type')->value][$entity->get('entity_id')->value][$entity->get('flag_id')->value] = $entity->get('flag_id')->value; + } + } + } + + /** + * {@inheritdoc} + */ + protected function doDelete($entities) { + + parent::doDelete($entities); + + /** @var \Drupal\Core\Entity\ContentEntityInterface[] $entities */ + foreach ($entities as $entity) { + // After deleting a flagging, remove it from the cached flagging by entity if already in static cache. + if ($entity->get('global')->value) { + if (isset($this->globalFlagIdsByEntity[$entity->get('entity_type')->value][$entity->get('entity_id')->value][$entity->get('flag_id')->value])) { + unset($this->globalFlagIdsByEntity[$entity->get('entity_type')->value][$entity->get('entity_id')->value][$entity->get('flag_id')->value]); + } + } + else { + if (isset($this->flagIdsByEntity[$entity->get('uid')->target_id][$entity->get('entity_type')->value][$entity->get('entity_id')->value][$entity->get('flag_id')->value])) { + unset($this->flagIdsByEntity[$entity->get('uid')->target_id][$entity->get('entity_type')->value][$entity->get('entity_id')->value][$entity->get('flag_id')->value]); + } + } + } + } } diff --git a/tests/src/Kernel/FlagKernelTestBase.php b/tests/src/Kernel/FlagKernelTestBase.php index bdc36e9..8cf9fee 100644 --- a/tests/src/Kernel/FlagKernelTestBase.php +++ b/tests/src/Kernel/FlagKernelTestBase.php @@ -1,6 +1,7 @@ installSchema('system', ['sequences']); $this->installSchema('flag', ['flag_counts']); $this->installSchema('node', ['node_access']); + $this->installConfig(['filter', 'node']); $this->flagService = \Drupal::service('flag'); } diff --git a/tests/src/Kernel/FlaggingStorageTest.php b/tests/src/Kernel/FlaggingStorageTest.php new file mode 100644 index 0000000..e055f67 --- /dev/null +++ b/tests/src/Kernel/FlaggingStorageTest.php @@ -0,0 +1,69 @@ +account = $this->createUser(); + + $this->flag = $this->createFlag('node', ['article']); + + // A node to test with. + $this->createContentType(['type' => 'article']); + $this->node = $this->createNode(['type' => 'article']); + } + + /** + * Test that cache reset is working. + */ + public function testCacheReset() { + // Flag the node on behalf of the user. + $this->flagService->flag($this->flag, $this->node, $this->account); + $this->assertTrue($this->flag->isFlagged($this->node, $this->account)); + + // Unflag and verify that the internal caches have been reset. + $this->flagService->unflag($this->flag, $this->node, $this->account); + $this->assertFalse($this->flag->isFlagged($this->node, $this->account)); + } + +}