The Gitlab instance will be restarted on Monday April 28th at 2AM. There will be a short interruption of service.

Skip to content
Snippets Groups Projects
Commit 87961468 authored by Brad Busenius's avatar Brad Busenius Committed by Robert Lange
Browse files

Configurable top facet override of alphabetical sort for limits (#1807)

parent 5532f115
No related merge requests found
......@@ -256,6 +256,18 @@ translated_facets[] = callnumber-first:CallNumberFirst
; These facets will be displayed on the Home Page when FacetList is turned on in
; the content setting of the [HomePage] section of searches.ini. If this section
; is omitted, the [Advanced] section will be used instead.
; Override the alphabetical sorting for individual facets and display them at the
; top of the limits on the advanced search page. As an example, this could be used
; to display the most commonly searched languages above the rest. All following
; limits display in the natural sorted order.
;limitOrderOverride[language] = Icelandic::English::Spanish
;limitOrderOverride[format] = CD::DVD
; Optional delimiter to use in the limitOrderOverride settings above. When enabled,
; limits must be separated using the same character set here.
;limitDelimiter = "::"
[HomePage]
callnumber-first = "Call Number"
language = Language
......
......@@ -982,4 +982,21 @@ abstract class Options implements TranslatorAwareInterface
$this->autocompleteAutoSubmit = $searchSettings->Autocomplete->auto_submit
?? $this->autocompleteAutoSubmit;
}
/**
* Get advanced search limits that override the natural sorting to
* display at the top.
*
* @param string $limit advanced search limit
*
* @return array
*/
public function limitOrderOverride($limit)
{
$facetSettings = $this->configLoader->get($this->getFacetsIni());
$limits = $facetSettings->Advanced_Settings->limitOrderOverride ?? null;
$delimiter = $facetSettings->Advanced_Settings->limitDelimiter ?? '::';
$limitConf = $limits ? $limits->get($limit) : '';
return array_map('trim', explode($delimiter, $limitConf));
}
}
......@@ -16,16 +16,34 @@
<?php endforeach; ?>
<?php else: ?>
<?php
// Sort the current facet list alphabetically; we'll use this data
// Sort the current facet list alphabetically and filter items to
// the top if they appear in the config; we'll use this data
// along with the foreach below to display facet options in the
// correct order.
$conf = $this->options->limitOrderOverride($field);
$sorted = [];
$filtered = [];
foreach ($list['list'] as $i => $value) {
if (!empty($value['displayText'])) {
$sorted[$i] = $value['displayText'];
if (in_array($value['displayText'], $conf)) {
$filtered[$i] = $value['displayText'];
} else {
$sorted[$i] = $value['displayText'];
}
}
}
natcasesort($sorted);
// Order filtered items according to how they appear in the config.
$filterKeys = array_flip($conf);
uasort($filtered, function ($a, $b) use ($filterKeys) {
return $filterKeys[$a] <=> $filterKeys[$b];
});
// Combine filtered and sorted arrays so that the items in the config
// appear in order at the top and all other items appear afterwards
// sorted by natcasesort.
$sorted = $filtered + $sorted;
?>
<?php foreach ($sorted as $i => $display): ?>
<?php $value = $list['list'][$i]; ?>
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment