Skip to content

Commit

Permalink
[FINNA-2953] Add a virtual filter for showing/hiding component parts. (
Browse files Browse the repository at this point in the history
  • Loading branch information
EreMaijala authored Dec 16, 2024
1 parent 3592cf8 commit 5aeb0cd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
6 changes: 6 additions & 0 deletions local/config/finna/facets.ini.sample
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ online_boolean:1 = online_material_facet_html
;free_online_boolean:1 = free_online_material_facet_html
source_available_str_mv:* = available_locally_facet_html
;hires_image_boolean:1 = high_resolution_image_available_html
; Either of the following mappings can be used together with the
; hide_component_parts setting in searches.ini to give the user the option to
; reverse the default behavior. Note that the descriptions here are provided
; as an example only, and there are no existing translations for them.
;finna.include_hidden_parts:1 = "Include contained records"
;finna.include_hidden_parts:0 = "Exclude contained records"

; 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.
Expand Down
3 changes: 2 additions & 1 deletion local/config/finna/searches.ini.sample
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ snippets = true
; controls its default state: on (true) or off (false).
retain_filters_by_default = false

; Whether to hide merged component parts
; Whether to hide merged component parts. See also finna.include_hidden_parts
; in CheckboxFacets section of facets.ini for user-selectable options.
hide_component_parts = true

; Maximum result to display
Expand Down
58 changes: 37 additions & 21 deletions module/Finna/src/Finna/Search/Solr/SolrExtensionsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* PHP version 8
*
* Copyright (C) The National Library of Finland 2013-2023.
* Copyright (C) The National Library of Finland 2013-2024.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
Expand Down Expand Up @@ -33,7 +33,6 @@
use Laminas\EventManager\SharedEventManagerInterface;
use Laminas\ServiceManager\ServiceLocatorInterface;
use VuFindSearch\Query\Query;
use VuFindSearch\Query\QueryGroup;

use function count;
use function in_array;
Expand Down Expand Up @@ -354,28 +353,45 @@ protected function addAuthorIdAlternatives(EventInterface $event)
*/
protected function addHiddenComponentPartFilter(EventInterface $event)
{
$config = $this->serviceLocator->get(\VuFind\Config\PluginManager::class);
$searchConfig = $config->get($this->searchConfig);
if (
isset($searchConfig->General->hide_component_parts)
&& $searchConfig->General->hide_component_parts
) {
$command = $event->getParam('command');
$params = $command->getSearchParameters();
if ($params) {
// Check that search is not for a known record id
$query = method_exists($command, 'getQuery')
? $command->getQuery()
: null;
if (
!$query
|| $query instanceof QueryGroup
|| ($query instanceof Query && $query->getHandler() !== 'id')
) {
$params->add('fq', '-hidden_component_boolean:true');
$hideHiddenComponentsPart = null;
$command = $event->getParam('command');
$params = $command->getSearchParameters();
if (!$params) {
return;
}

// Check that search is not for a known record id
$query = method_exists($command, 'getQuery') ? $command->getQuery() : null;
if ($query instanceof Query && $query->getHandler() === 'id') {
return;
}

if ($fq = $params->get('fq')) {
// Check for a filter parameter:
$optionMappings = [
'finna.include_hidden_parts:"1"' => false,
'finna.include_hidden_parts:"0"' => true,
];
foreach ($optionMappings as $filter => $value) {
if (false !== ($key = array_search($filter, $fq))) {
$hideHiddenComponentsPart = $value;
unset($fq[$key]);
$params->set('fq', $fq);
}
}
}

if (null === $hideHiddenComponentsPart) {
// Check for config:
$config = $this->serviceLocator->get(\VuFind\Config\PluginManager::class);
$searchConfig = $config->get($this->searchConfig);
$hideHiddenComponentsPart = $searchConfig->General->hide_component_parts ?? false;
}

// Add the parameter if needed:
if ($hideHiddenComponentsPart) {
$params->add('fq', '-hidden_component_boolean:true');
}
}

/**
Expand Down

0 comments on commit 5aeb0cd

Please sign in to comment.