Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions modules/next/src/Entity/NextSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
}
Expand Down
6 changes: 3 additions & 3 deletions modules/next/src/Entity/NextSiteInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
101 changes: 101 additions & 0 deletions modules/next/src/Plugin/Next/Revalidator/CacheTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace Drupal\next\Plugin\Next\Revalidator;

use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Utility\Error;
use Drupal\next\Event\EntityActionEvent;
use Drupal\next\Plugin\ConfigurableRevalidatorBase;
use Drupal\next\Plugin\RevalidatorInterface;
use Symfony\Component\HttpFoundation\Response;

/**
* Cache tag based on-demand revalidation plugin.
*
* @Revalidator(
* id = "cache_tag",
* label = "Cache Tag",
* description = "Cache tag based on-demand revalidation."
* )
*/
class CacheTag extends ConfigurableRevalidatorBase implements RevalidatorInterface {

/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [];
}

/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
return $form;
}

/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
// No configuration form.
}

/**
* {@inheritdoc}
* @throws \Drupal\Core\Entity\EntityMalformedException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function revalidate(EntityActionEvent $event): bool {
$revalidated = FALSE;
$entity = $event->getEntity();
$sites = $event->getSites();

if (!($entity instanceof FieldableEntityInterface)) {
return FALSE;
}

$cache_tags = implode(',', $entity->getCacheTags());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@minnur I think the entity list tags we still have to add manually, at least in my test for nodes for example $entity->getCacheTags only gave node:8

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know about this, but apparently if we do this:

$entity->getEntityType()->getListCacheTags();

We should get the correct list tag. The one with the bundle at the end we still need to create manually.

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good find! I added that to the plugin. See db2690b
I noticed $entity->getEntityType()->getListCacheTags(); does not have entity_type_list:bundle. So I looked at \Drupal\Entity\EntityBase class and found a method getListCacheTagsToInvalidate() that adds that to the invalidation list and implemented the same logic in the plugin.


/** @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;
}

}
3 changes: 2 additions & 1 deletion modules/next/src/Plugin/Next/Revalidator/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down
8 changes: 4 additions & 4 deletions modules/next/tests/src/Kernel/Entity/NextSiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function test() {
}

/**
* @covers ::getRevalidateUrlForPath
* @covers ::buildRevalidateUrl
*/
public function testGetRevalidateUrlForPath() {
$marketing = NextSite::create([
Expand All @@ -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());
}

}
Loading