From b553f2cd2b1869639819b1d2298f80a42fbd1432 Mon Sep 17 00:00:00 2001 From: dannylamb Date: Wed, 18 Dec 2019 12:52:51 -0400 Subject: [PATCH] Jsonld alter caching (#747) * Invalidating cache appropriately when context that affect jsonld change * Tests, coding standards --- islandora.module | 11 +++++++++++ .../Functional/JsonldSelfReferenceReactionTest.php | 8 ++++++++ tests/src/Functional/JsonldTypeAlterReactionTest.php | 4 ++++ 3 files changed, 23 insertions(+) diff --git a/islandora.module b/islandora.module index 920f1eb0..2cae3270 100644 --- a/islandora.module +++ b/islandora.module @@ -14,6 +14,7 @@ * @author Diego Pino Navarro https://github.com/diegopino */ +use Drupal\Component\Plugin\Exception\PluginNotFoundException; use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Form\FormStateInterface; @@ -253,6 +254,16 @@ function islandora_jsonld_alter_normalized_array(EntityInterface $entity, array $context_manager = \Drupal::service('context.manager'); foreach ($context_manager->getActiveReactions('\Drupal\islandora\ContextReaction\NormalizerAlterReaction') as $reaction) { $reaction->execute($entity, $normalized, $context); + foreach ($context_manager->getActiveContexts() as $context_config) { + try { + if ($context_config->getReaction($reaction->getPluginId())) { + $context['cacheability']->addCacheTags($context_config->getCacheTags()); + }; + } + catch (PluginNotFoundException $e) { + // Squash :(. + } + } } } diff --git a/tests/src/Functional/JsonldSelfReferenceReactionTest.php b/tests/src/Functional/JsonldSelfReferenceReactionTest.php index 6311cb17..0026b289 100644 --- a/tests/src/Functional/JsonldSelfReferenceReactionTest.php +++ b/tests/src/Functional/JsonldSelfReferenceReactionTest.php @@ -98,6 +98,10 @@ class JsonldSelfReferenceReactionTest extends IslandoraFunctionalTestBase { $this->assertSession() ->pageTextContains("The context $context_name has been saved"); + // The first time a Context is saved, you need to clear the cache. + // Subsequent changes to the context don't need a cache rebuild, though. + drupal_flush_all_caches(); + $new_contents = $this->drupalGet($url . '?_format=jsonld'); $json = \GuzzleHttp\json_decode($new_contents, TRUE); $this->assertEquals( @@ -177,6 +181,10 @@ class JsonldSelfReferenceReactionTest extends IslandoraFunctionalTestBase { $this->assertSession() ->pageTextContains("The context $context_name has been saved"); + // The first time a Context is saved, you need to clear the cache. + // Subsequent changes to the context don't need a cache rebuild, though. + drupal_flush_all_caches(); + $new_contents = $this->drupalGet($media_url . '?_format=jsonld'); $json = \GuzzleHttp\json_decode($new_contents, TRUE); $this->assertEquals( diff --git a/tests/src/Functional/JsonldTypeAlterReactionTest.php b/tests/src/Functional/JsonldTypeAlterReactionTest.php index 29232ca1..0232518b 100644 --- a/tests/src/Functional/JsonldTypeAlterReactionTest.php +++ b/tests/src/Functional/JsonldTypeAlterReactionTest.php @@ -75,6 +75,10 @@ class JsonldTypeAlterReactionTest extends JsonldSelfReferenceReactionTest { $this->getSession()->getPage()->findById("edit-conditions-entity-bundle-context-mapping-node")->selectOption("@node.node_route_context:node"); $this->getSession()->getPage()->pressButton(t('Save and continue')); + // The first time a Context is saved, you need to clear the cache. + // Subsequent changes to the context don't need a cache rebuild, though. + drupal_flush_all_caches(); + // Check for the new @type from the field_type_predicate value. $new_contents = $this->drupalGet($url . '?_format=jsonld'); $json = \GuzzleHttp\json_decode($new_contents, TRUE);