From e16a6b9de9d969f37cdc8ab53c92c96048be1ec2 Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Mon, 18 Dec 2023 09:10:43 +0200 Subject: [PATCH 1/3] UHF-9454: Fix address search caching for translated entities --- .../src/Plugin/views/filter/AddressSearch.php | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/modules/helfi_address_search/src/Plugin/views/filter/AddressSearch.php b/modules/helfi_address_search/src/Plugin/views/filter/AddressSearch.php index eac1d0b..5cdf275 100644 --- a/modules/helfi_address_search/src/Plugin/views/filter/AddressSearch.php +++ b/modules/helfi_address_search/src/Plugin/views/filter/AddressSearch.php @@ -7,6 +7,8 @@ use Drupal\Component\Serialization\Json; use Drupal\Component\Utility\Xss; use Drupal\Core\Entity\ContentEntityInterface; +use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\TranslatableInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\LanguageInterface; use Drupal\views\Plugin\views\filter\FilterPluginBase; @@ -90,9 +92,9 @@ public static function sortByAddress(ViewExecutable $view): ViewExecutable { (float) $result->_entity->get('latitude')->getString(), (float) $result->_entity->get('longitude')->getString()); - // The entity should not be cached since it relies on high-cardinality - // user input. - $result->_entity->mergeCacheMaxAge(0); + // The entity should not be cached if it wants to render the computed + // distance field (since the value relies on high-cardinality user input). + self::entityCacheKillSwitch($result->_entity); // Set the distance to computed field. $result->_entity->set('distance', $distances[$result->_entity->get('id')->getString()]); @@ -237,4 +239,21 @@ protected static function setSearchStatus(array $element, bool $succeed): array return $element; } + /** + * Disable caching for given entity. + * + * @param EntityInterface $entity + * The entity. + */ + private static function entityCacheKillSwitch(EntityInterface $entity): void { + if ($entity instanceof TranslatableInterface) { + foreach ($entity->getTranslationLanguages() as $language) { + $entity->getTranslation($language->getId())->mergeCacheMaxAge(0); + } + } + else { + $entity->mergeCacheMaxAge(0); + } + } + } From a5037bc6741fff97bb9e16a7ed8df9e308d691d2 Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Tue, 19 Dec 2023 10:43:22 +0200 Subject: [PATCH 2/3] UHF-9454: PHPCS fix --- .../src/Plugin/views/filter/AddressSearch.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/helfi_address_search/src/Plugin/views/filter/AddressSearch.php b/modules/helfi_address_search/src/Plugin/views/filter/AddressSearch.php index 5cdf275..826f45d 100644 --- a/modules/helfi_address_search/src/Plugin/views/filter/AddressSearch.php +++ b/modules/helfi_address_search/src/Plugin/views/filter/AddressSearch.php @@ -242,7 +242,7 @@ protected static function setSearchStatus(array $element, bool $succeed): array /** * Disable caching for given entity. * - * @param EntityInterface $entity + * @param \Drupal\Core\Entity\EntityInterface $entity * The entity. */ private static function entityCacheKillSwitch(EntityInterface $entity): void { From 215ec59f0bb44cd8cab2af794b9a4a35b4ed1173 Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Tue, 19 Dec 2023 11:30:18 +0200 Subject: [PATCH 3/3] UHF-9454: Fix tests --- tests/src/Functional/ServiceTranslationTest.php | 4 ++-- tests/src/Functional/UnitTranslationTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/src/Functional/ServiceTranslationTest.php b/tests/src/Functional/ServiceTranslationTest.php index 58e8dc9..1b0ecfb 100644 --- a/tests/src/Functional/ServiceTranslationTest.php +++ b/tests/src/Functional/ServiceTranslationTest.php @@ -45,7 +45,7 @@ public function testPublish() : void { $this->drupalGet(Url::fromRoute('entity.tpr_service.edit_form', ['tpr_service' => 1]), [ 'query' => ['language' => 'fi'], ]); - $this->assertSession()->fieldValueEquals('content_translation[status]', $expected_status); + $this->assertSession()->fieldValueEquals('content_translation[status]', (string) $expected_status); } // Run migrate to update existing service entity and translations. @@ -65,7 +65,7 @@ public function testPublish() : void { $this->drupalGet(Url::fromRoute('entity.tpr_service.edit_form', ['tpr_service' => 1]), [ 'query' => ['language' => $language], ]); - $this->assertSession()->fieldValueEquals('content_translation[status]', $expected_status); + $this->assertSession()->fieldValueEquals('content_translation[status]', (string) $expected_status); } } } diff --git a/tests/src/Functional/UnitTranslationTest.php b/tests/src/Functional/UnitTranslationTest.php index c62abc9..932ce89 100644 --- a/tests/src/Functional/UnitTranslationTest.php +++ b/tests/src/Functional/UnitTranslationTest.php @@ -45,7 +45,7 @@ public function testPublish() : void { $this->drupalGet(Url::fromRoute('entity.tpr_unit.edit_form', ['tpr_unit' => 1]), [ 'query' => ['language' => 'fi'], ]); - $this->assertSession()->fieldValueEquals('content_translation[status]', $expected_status); + $this->assertSession()->fieldValueEquals('content_translation[status]', (string) $expected_status); } // Run migrate to update existing unit entity andtranslations. @@ -65,7 +65,7 @@ public function testPublish() : void { $this->drupalGet(Url::fromRoute('entity.tpr_unit.edit_form', ['tpr_unit' => 1]), [ 'query' => ['language' => $language], ]); - $this->assertSession()->fieldValueEquals('content_translation[status]', $expected_status); + $this->assertSession()->fieldValueEquals('content_translation[status]', (string) $expected_status); } } }