Skip to content

Commit

Permalink
Merge pull request #165 from City-of-Helsinki/UHF-9454-address-search…
Browse files Browse the repository at this point in the history
…-caching

UHF-9454: Fix address search caching for translated entities [part 2]
  • Loading branch information
hyrsky authored Dec 19, 2023
2 parents 6d5e7f0 + 215ec59 commit cf8fd5d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()]);
Expand Down Expand Up @@ -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);
}
}

}
4 changes: 2 additions & 2 deletions tests/src/Functional/ServiceTranslationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/src/Functional/UnitTranslationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}
}
}
Expand Down

0 comments on commit cf8fd5d

Please sign in to comment.