Skip to content

Commit d4718e8

Browse files
authored
Merge pull request #627 from itk-dev/hotfix/invalidate-on-flag-change
Added cache invalidation on flag/unflag
2 parents d700b7d + 8ed11c7 commit d4718e8

3 files changed

Lines changed: 60 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
88

9+
## [4.14.6] - 2026-01-29
10+
11+
* [PR-626](https://github.com/itk-dev/deltag.aarhus.dk/pull/623)
12+
Invalidate node cache when node is flagged.
13+
914
## [4.14.5] - 2026-01-29
1015

1116
* [PR-623](https://github.com/itk-dev/deltag.aarhus.dk/pull/623)

web/modules/custom/hoeringsportal_dialogue/hoeringsportal_dialogue.services.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ services:
1414
Drupal\hoeringsportal_dialogue\EventSubscriber\AnonymousEditSubscriber:
1515
tags:
1616
- { name: event_subscriber }
17+
18+
Drupal\hoeringsportal_dialogue\EventSubscriber\FlagCacheInvalidationSubscriber:
19+
tags:
20+
- { name: event_subscriber }
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Drupal\hoeringsportal_dialogue\EventSubscriber;
6+
7+
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
8+
use Drupal\flag\Event\FlaggingEvent;
9+
use Drupal\flag\Event\UnflaggingEvent;
10+
use Drupal\node\NodeInterface;
11+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
12+
13+
/**
14+
* Invalidates node cache when flagged or unflagged.
15+
*/
16+
final readonly class FlagCacheInvalidationSubscriber implements EventSubscriberInterface {
17+
18+
public function __construct(
19+
private CacheTagsInvalidatorInterface $cacheTagsInvalidator,
20+
) {}
21+
22+
/**
23+
* Invalidates the cache for the flagged/unflagged node.
24+
*/
25+
public function onFlagChange(FlaggingEvent|UnflaggingEvent $event): void {
26+
if ($event instanceof FlaggingEvent) {
27+
$flagging = $event->getFlagging();
28+
$entity = $flagging->getFlaggable();
29+
}
30+
else {
31+
$flaggings = $event->getFlaggings();
32+
$flagging = reset($flaggings);
33+
$entity = $flagging->getFlaggable();
34+
}
35+
36+
if ($entity instanceof NodeInterface) {
37+
$this->cacheTagsInvalidator->invalidateTags($entity->getCacheTags());
38+
}
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public static function getSubscribedEvents(): array {
45+
return [
46+
'flag.entity_flagged' => ['onFlagChange'],
47+
'flag.entity_unflagged' => ['onFlagChange'],
48+
];
49+
}
50+
51+
}

0 commit comments

Comments
 (0)