Skip to content
Snippets Groups Projects
Commit 99ea77ac authored by Frank Mably's avatar Frank Mably
Browse files

Merge branch '3527692-glossary-caches-should' into '4.x'

Issue #3527692 by mably: Glossary caches should be purged automatically when the configuration is updated

See merge request !44
parents 0392abc4 df18d8ca
No related branches found
No related tags found
No related merge requests found
Pipeline #511378 passed
......@@ -234,9 +234,11 @@ class TermGlossaryManager implements TermGlossaryManagerInterface {
? $root_entity->language()->getId()
: $this->languageManager->getCurrentLanguage()->getId();
$cache_tags = [];
$term_list = [];
foreach ($vocabularies as $vocabulary) {
$term_list += $this->getTerms($vocabulary, $langcode);
$cache_tags[] = "taxonomy_term_list:{$vocabulary}";
}
$is_single_match_per_field = $this->config->get('single_match') ?? FALSE;
......@@ -341,7 +343,7 @@ class TermGlossaryManager implements TermGlossaryManagerInterface {
if ($ctx->tagIndex === 0) {
// No replacement has occurred, nothing to do.
return FALSE;
return ['cache_tags' => $cache_tags];
}
else {
// Replace all placeholders by their corresponding tags.
......@@ -362,7 +364,7 @@ class TermGlossaryManager implements TermGlossaryManagerInterface {
},
Html::serialize($html_dom),
);
return ['html' => $html, 'count' => $ctx->tagIndex];
return ['html' => $html, 'count' => $ctx->tagIndex, 'cache_tags' => $cache_tags];
}
}
}
......@@ -403,6 +405,13 @@ class TermGlossaryManager implements TermGlossaryManagerInterface {
return $tag_markup;
}
/**
* {@inheritDoc}
*/
public function getConfig(): ?ImmutableConfig {
return $this->config;
}
/**
* {@inheritDoc}
*/
......@@ -527,7 +536,7 @@ class TermGlossaryManager implements TermGlossaryManagerInterface {
$cid = $this->getCacheId($vocabulary, $langcode);
// The cache will be invalidated when a term from this vocabulary is added,
// updated, or deleted, due to the use of the following cache tag.
$cache_tags = ["taxonomy_term_list:{$vocabulary}"];
$cache_tags = array_merge($this->config->getCacheTags(), ["taxonomy_term_list:{$vocabulary}"]);
$this->cache->set($cid, $terms_array, CacheBackendInterface::CACHE_PERMANENT, $cache_tags);
return $terms_array;
}
......
......@@ -2,6 +2,7 @@
namespace Drupal\term_glossary\Service;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FormatterInterface;
......@@ -33,6 +34,14 @@ interface TermGlossaryManagerInterface {
*/
public function attachLibrariesAndSettings(&$variables);
/**
* Gets the immutable configuration object.
*
* @return \Drupal\Core\Config\ImmutableConfig|null
* The immutable configuration object or NULL if not available.
*/
public function getConfig(): ?ImmutableConfig;
/**
* Get configuration.
*
......
......@@ -8,6 +8,7 @@
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FormatterInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Routing\RouteMatchInterface;
/**
......@@ -119,12 +120,22 @@ function term_glossary_preprocess_field(&$variables) {
$result = $glossaryManager->replaceFieldValue(
$markup->jsonSerialize(), $vocabularies, $root_entity);
if (is_array($result)) {
$replacements += $result['count'];
// Replace field markup content.
$variables['items'][$key]['content'] = [
'#type' => 'markup',
'#markup' => $result['html'],
];
if (isset($result['html'])) {
$replacements += $result['count'];
// Replace field markup content.
$variables['items'][$key]['content'] = [
'#type' => 'markup',
'#markup' => $result['html'],
];
}
// Attach cache tags to the render array.
$bubbleable_metadata = new BubbleableMetadata();
$cache_tags = $glossaryManager->getConfig()->getCacheTags();
if (!empty($result['cache_tags'])) {
$cache_tags = array_merge($cache_tags, $result['cache_tags']);
}
$bubbleable_metadata->setCacheTags($cache_tags);
$bubbleable_metadata->applyTo($variables);
}
}
// Inject libraries and settings when replacements occurred.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment