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..826f45d 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 \Drupal\Core\Entity\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); + } + } + } 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); } } }