Problem/Motivation
When a single filter is used on a page the languages are detected and processed correctly.
When multiple filters exist on the same page then the last filter encountered on the page will dictate the languages in the `drupalSettings` array that then get passed to the JavaScript.
Steps to reproduce
Let's say we had a node with the language `php`. This is displayed on the same page as a node that also uses the filter and has the language `sql`. When this happens the last encountered filter sets the final list of languages sent to the JavaScript. This means we'll either get `sql` or `php`, but not both.
The two pages displayed side by side might be via a listing view or included through entity reference.
Proposed resolution
Ultimately, the list of languages should be based on everything found on the page and not just those found within a single input filter.
I have tried a couple of things to get this working, but ultimately they revolved around the HighlightJs filter object being retailed throughout the filter process, which it isn't. A new HighlightJs filter is created for every pass, which means that they have no knowledge of what happened before.
Normally, I would use drupal_static() for this situation, but it looks like this function will likely become deprecated soon (see
https://www.drupal.org/node/2661204 β
). So, a longer term solution would be good.
I found a solution using the `state` service that allowed the currently loaded languages to be stored and merged with any new languages found.
$state = \Drupal::service('state');
$stateLanguages = $state->get('highlightjs_languages');
$stateLanguages = array_values(array_unique(array_merge($languages, $stateLanguages ?: [])));
$state->set('highlightjs_languages', $stateLanguages);
If this seems like a good solution then I'll get this into a merge request :)
Remaining tasks
Write a test for this situation.