diff --git a/modules/next/src/Entity/NextSite.php b/modules/next/src/Entity/NextSite.php index 5a75cd3f8..098f06a26 100644 --- a/modules/next/src/Entity/NextSite.php +++ b/modules/next/src/Entity/NextSite.php @@ -265,22 +265,15 @@ public function getLiveUrlForEntity(EntityInterface $entity): ?Url { /** * {@inheritdoc} */ - public function getRevalidateUrlForPath(string $path): ?Url { + public function buildRevalidateUrl(array $query = []): ?Url { $revalidate_url = $this->getRevalidateUrl(); - if (!$revalidate_url) { return NULL; } - - $query = [ - 'path' => $path, - ]; - if ($secret = $this->getRevalidateSecret()) { $query['secret'] = $secret; } - - return Url::fromUri($this->getRevalidateUrl(), [ + return Url::fromUri($revalidate_url, [ 'query' => $query, ]); } diff --git a/modules/next/src/Entity/NextSiteInterface.php b/modules/next/src/Entity/NextSiteInterface.php index c09a21c0b..95a69a84c 100644 --- a/modules/next/src/Entity/NextSiteInterface.php +++ b/modules/next/src/Entity/NextSiteInterface.php @@ -131,12 +131,12 @@ public function setRevalidateSecret(string $revalidate_secret): self; /** * Returns the revalidate url for given path. * - * @param string $path - * The path. + * @param array $query + * The revalidate URL query parameters. * * @return \Drupal\Core\Url|null * The revalidate url. */ - public function getRevalidateUrlForPath(string $path): ?Url; + public function buildRevalidateUrl(array $query = []): ?Url; } diff --git a/modules/next/src/Plugin/Next/Revalidator/CacheTag.php b/modules/next/src/Plugin/Next/Revalidator/CacheTag.php new file mode 100644 index 000000000..9725ce690 --- /dev/null +++ b/modules/next/src/Plugin/Next/Revalidator/CacheTag.php @@ -0,0 +1,107 @@ +getEntity(); + $sites = $event->getSites(); + + if (!($entity instanceof FieldableEntityInterface)) { + return FALSE; + } + + // Get all available cache tags (including list tags). + $list_tags = $entity->getEntityType()->getListCacheTags(); + if ($entity->getEntityType()->hasKey('bundle')) { + $list_tags[] = $entity->getEntityTypeId() . '_list:' . $entity->bundle(); + } + $combined_tags = array_merge($entity->getCacheTags(), $list_tags); + $cache_tags = implode(',', $combined_tags); + + /** @var \Drupal\next\Entity\NextSite $site */ + foreach ($sites as $site) { + try { + $revalidate_url = $site->buildRevalidateUrl(['tags' => $cache_tags]); + if (!$revalidate_url) { + throw new \Exception('No revalidate url set.'); + } + + if ($this->nextSettingsManager->isDebug()) { + $this->logger->notice('(@action): Revalidating tags %list for the site %site. URL: %url', [ + '@action' => $event->getAction(), + '%list' => $cache_tags, + '%site' => $site->label(), + '%url' => $revalidate_url->toString(), + ]); + } + + $response = $this->httpClient->request('GET', $revalidate_url->toString()); + if ($response && $response->getStatusCode() === Response::HTTP_OK) { + if ($this->nextSettingsManager->isDebug()) { + $this->logger->notice('(@action): Successfully revalidated tags %list for the site %site. URL: %url', [ + '@action' => $event->getAction(), + '%list' => $cache_tags, + '%site' => $site->label(), + '%url' => $revalidate_url->toString(), + ]); + } + + $revalidated = TRUE; + } + } + catch (\Exception $exception) { + Error::logException($this->logger, $exception); + $revalidated = FALSE; + } + } + + return $revalidated; + } + +} diff --git a/modules/next/src/Plugin/Next/Revalidator/Path.php b/modules/next/src/Plugin/Next/Revalidator/Path.php index 559ef03fa..ececdba74 100644 --- a/modules/next/src/Plugin/Next/Revalidator/Path.php +++ b/modules/next/src/Plugin/Next/Revalidator/Path.php @@ -84,9 +84,10 @@ public function revalidate(EntityActionEvent $event): bool { } foreach ($paths as $path) { + /** @var \Drupal\next\Entity\NextSite $site */ foreach ($sites as $site) { try { - $revalidate_url = $site->getRevalidateUrlForPath($path); + $revalidate_url = $site->buildRevalidateUrl(['path' => $path]); if (!$revalidate_url) { throw new \Exception('No revalidate url set.'); diff --git a/modules/next/tests/src/Kernel/Entity/NextSiteTest.php b/modules/next/tests/src/Kernel/Entity/NextSiteTest.php index abeb96755..18e676f3b 100644 --- a/modules/next/tests/src/Kernel/Entity/NextSiteTest.php +++ b/modules/next/tests/src/Kernel/Entity/NextSiteTest.php @@ -117,7 +117,7 @@ public function test() { } /** - * @covers ::getRevalidateUrlForPath + * @covers ::buildRevalidateUrl */ public function testGetRevalidateUrlForPath() { $marketing = NextSite::create([ @@ -129,13 +129,13 @@ public function testGetRevalidateUrlForPath() { ]); $marketing->save(); - $this->assertNull($marketing->getRevalidateUrlForPath('/foo')); + $this->assertNull($marketing->buildRevalidateUrl(['path' => '/foo'])); $marketing->setRevalidateUrl('http://example.com/api/revalidate'); - $this->assertSame('http://example.com/api/revalidate?path=/foo', $marketing->getRevalidateUrlForPath('/foo')->toString()); + $this->assertSame('http://example.com/api/revalidate?path=/foo', $marketing->buildRevalidateUrl(['path' => '/foo'])->toString()); $marketing->setRevalidateSecret('12345'); - $this->assertSame('http://example.com/api/revalidate?path=/foo&secret=12345', $marketing->getRevalidateUrlForPath('/foo')->toString()); + $this->assertSame('http://example.com/api/revalidate?path=/foo&secret=12345', $marketing->buildRevalidateUrl(['path' => '/foo'])->toString()); } }