From 89e9a492fe18a0a1a92768e793a8a5c068b40ba1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuli=20Sillanp=C3=A4=C3=A4?= <samuli.sillanpaa@helsinki.fi> Date: Thu, 10 Oct 2019 21:27:07 +0300 Subject: [PATCH] Add support for configuring available sort options for facets (#1452) --- config/vufind/facets.ini | 16 ++++++++++++++ .../src/VuFind/Controller/AbstractSearch.php | 2 +- .../VuFind/src/VuFind/Search/Base/Options.php | 22 ++++++++++++++++--- .../VuFind/src/VuFind/Search/Solr/Options.php | 3 +-- 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/config/vufind/facets.ini b/config/vufind/facets.ini index 5dfca94afc9..e3c1492f9bf 100644 --- a/config/vufind/facets.ini +++ b/config/vufind/facets.ini @@ -107,6 +107,22 @@ dateRange[] = publishDate [CheckboxFacets] ;edition:1st* = "First Edition" ; Contrived hypothetical example +; Available sort options when listing all facets from Sidefacets. +; +; Each configuration option targets a search class and a facet field. +; All facet fields for a search class can be targeted using the wildcard '*'. +; Sort options are given as a comma-separated list of "<sort-field>=<label>" entries, +; where <sort-field> is either 'count' or 'index' and <label> the translation +; key for the option. +[AvailableFacetSortOptions] +; By default all Solr facets can be sorted by count and alphabetically. + +; Example: sort Solr author_facet by count only. +; Solr[author_facet] = "count=sort_count" + +; Example: sort Solr author_facet only alphabetically +; Solr[author_facet] = "index=sort_alphabetic" + ; These settings affect the way the [Results] facets are displayed ; If using facets at the top of search results you have more room for text. [Results_Settings] diff --git a/module/VuFind/src/VuFind/Controller/AbstractSearch.php b/module/VuFind/src/VuFind/Controller/AbstractSearch.php index f7714bc4f42..5c9c50099eb 100644 --- a/module/VuFind/src/VuFind/Controller/AbstractSearch.php +++ b/module/VuFind/src/VuFind/Controller/AbstractSearch.php @@ -718,7 +718,7 @@ class AbstractSearch extends AbstractBase $facet = $this->params()->fromQuery('facet'); $page = (int)$this->params()->fromQuery('facetpage', 1); $options = $results->getOptions(); - $facetSortOptions = $options->getFacetSortOptions(); + $facetSortOptions = $options->getFacetSortOptions($facet); $sort = $this->params()->fromQuery('facetsort', null); if ($sort === null || !in_array($sort, array_keys($facetSortOptions))) { $sort = empty($facetSortOptions) diff --git a/module/VuFind/src/VuFind/Search/Base/Options.php b/module/VuFind/src/VuFind/Search/Base/Options.php index f53294154b3..01eb5f3928e 100644 --- a/module/VuFind/src/VuFind/Search/Base/Options.php +++ b/module/VuFind/src/VuFind/Search/Base/Options.php @@ -292,6 +292,20 @@ abstract class Options implements TranslatorAwareInterface { $this->limitOptions = [$this->defaultLimit]; $this->setConfigLoader($configLoader); + + $id = $this->getSearchClassId(); + $facetSettings = $configLoader->get($this->facetsIni); + if (isset($facetSettings->AvailableFacetSortOptions[$id])) { + foreach ($facetSettings->AvailableFacetSortOptions[$id]->toArray() + as $facet => $sortOptions + ) { + $this->facetSortOptions[$facet] = []; + foreach (explode(',', $sortOptions) as $fieldAndLabel) { + list($field, $label) = explode('=', $fieldAndLabel); + $this->facetSortOptions[$facet][$field] = $label; + } + } + } } /** @@ -473,13 +487,15 @@ abstract class Options implements TranslatorAwareInterface } /** - * Get an array of sort options for facets. + * Get an array of sort options for a facet. + * + * @param string $facet Facet * * @return array */ - public function getFacetSortOptions() + public function getFacetSortOptions($facet = '*') { - return $this->facetSortOptions; + return $this->facetSortOptions[$facet] ?? $this->facetSortOptions['*'] ?? []; } /** diff --git a/module/VuFind/src/VuFind/Search/Solr/Options.php b/module/VuFind/src/VuFind/Search/Solr/Options.php index d15add0c4e2..2ddac83d955 100644 --- a/module/VuFind/src/VuFind/Search/Solr/Options.php +++ b/module/VuFind/src/VuFind/Search/Solr/Options.php @@ -46,8 +46,7 @@ class Options extends \VuFind\Search\Base\Options * @var array */ protected $facetSortOptions = [ - 'count' => 'sort_count', - 'index' => 'sort_alphabetic' + '*' => ['count' => 'sort_count', 'index' => 'sort_alphabetic'] ]; /** -- GitLab