diff --git a/tests/features/glossary/glossary.feature b/tests/features/glossary/glossary.feature
index c356023fc0..528de6f874 100644
--- a/tests/features/glossary/glossary.feature
+++ b/tests/features/glossary/glossary.feature
@@ -258,16 +258,17 @@ Feature: As a moderator or group facilitator I want to be able to add, edit and
| mod | moderator |
| fac1 | |
And the following collections:
- | title | description | state |
- | Collection With Glossary | The Battle of Evermore of the battle and the BATTLE. Call it everMore or EVERmore | validated |
- | The Other Collection | desc | validated |
+ | title | description | state |
+ | Collection With Glossary | The Battle of Evermore of the batTle and the BATTLE. Call it everMore or EVERmore. Also, battle again. | validated |
+ | The Other Collection | desc | validated |
And the following collection user membership:
| collection | user | roles |
| Collection With Glossary | fac | facilitator |
| The Other Collection | fac1 | facilitator |
And glossary content:
- | title | synonyms | definition | collection |
- | battle | evermore | def | Collection With Glossary |
+ | title | synonyms | definition | collection |
+ | battle | evermore | def | Collection With Glossary |
+ | battle again | | Not to be confused with a battle of sheep | Collection With Glossary |
Given I am an anonymous user
When I go to the "Collection With Glossary" collection
@@ -304,7 +305,10 @@ Feature: As a moderator or group facilitator I want to be able to add, edit and
# Only the first occurrence should be highlighted.
And I should see the link "Battle"
And I should see the link "Evermore"
- But I should not see the link "battle"
+ And I should see the link "battle again"
+ And the response should contain "Battle"
+ And the response should contain "battle again"
+ But I should not see the link "batTle"
And I should not see the link "BATTLE"
And I should not see the link "everMore"
And I should not see the link "EVERmore"
@@ -315,11 +319,12 @@ Feature: As a moderator or group facilitator I want to be able to add, edit and
# All terms should be highlighted.
Then I should see the link "Battle"
- And I should see the link "battle"
+ And I should see the link "batTle"
And I should see the link "BATTLE"
And I should see the link "Evermore"
And I should see the link "everMore"
And I should see the link "EVERmore"
+ And I should see the link "battle again"
When I click "Glossary"
Then I should see the link "Glossary settings"
diff --git a/web/modules/custom/collection/src/Plugin/Filter/Glossary.php b/web/modules/custom/collection/src/Plugin/Filter/Glossary.php
index 1f46093ccc..6ccd79826a 100644
--- a/web/modules/custom/collection/src/Plugin/Filter/Glossary.php
+++ b/web/modules/custom/collection/src/Plugin/Filter/Glossary.php
@@ -104,6 +104,12 @@ public function process($text, $langcode): FilterProcessResult {
/** @var \Drupal\Core\Cache\RefinableCacheableDependencyInterface $cache_metadata */
[$replacements, $cache_metadata] = $this->getReplacementsMap($collection);
+ // The reverse ksort will ensure that when, for any reason, there are terms
+ // that overlap together, like "exchange", "exchange currency" and "exchange
+ // platform", it is ensured, that terms with longer terms, are replaced
+ // first and the shorter terms are coming last.
+ krsort($replacements);
+
// This collection has no glossary term entries.
if (empty($replacements)) {
return $result->addCacheableDependency($cache_metadata);