Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UHF-9454: Fix address search caching for translated entities [part 2] #165

Merged
merged 3 commits into from
Dec 19, 2023
Merged
Changes from 1 commit
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
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 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);
}
}

}
Loading