Skip to content

Commit 856ce70

Browse files
Daniel BerthereauDaniel Berthereau
authored andcommitted
Added a view helper to display facets by multi-select.
1 parent 34ee478 commit 856ce70

File tree

6 files changed

+64
-2
lines changed

6 files changed

+64
-2
lines changed

asset/js/search.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,23 @@ $(document).ready(function() {
121121
$(this).closest('li').hide();
122122
var facetName = $(this).data('facetName');
123123
var facetValue = $(this).data('facetValue');
124-
$('.search-facet-item input').each(function() {
124+
$('.search-facet-item input:checked').each(function() {
125125
if ($(this).prop('name') === facetName
126126
&& $(this).prop('value') === String(facetValue)
127127
) {
128128
$(this).prop('checked', false);
129129
}
130130
});
131+
$('select.search-facet-items option:selected').each(function() {
132+
if ($(this).closest('select').prop('name') === facetName
133+
&& $(this).prop('value') === String(facetValue)
134+
) {
135+
$(this).prop('selected', false);
136+
if ($.isFunction($.fn.chosen)) {
137+
$(this).closest('select').trigger('chosen:updated');
138+
}
139+
}
140+
});
131141
});
132142

133143
$('.search-facets').on('change', 'input[type=checkbox]', function() {
@@ -136,6 +146,19 @@ $(document).ready(function() {
136146
}
137147
});
138148

149+
$('.search-facets').on('change', 'select', function() {
150+
if (!$('.apply-facets').length) {
151+
// Replace the current select args by new ones.
152+
let searchParams = new URLSearchParams(document.location.search);
153+
let selectName = $(this).prop('name');
154+
searchParams.delete(selectName);
155+
$(this).val().forEach((element, index) => {
156+
searchParams.set(selectName.substring(0, selectName.length - 2) + '[' + index + ']', element);
157+
});
158+
window.location = window.location.href + searchParams.toString();
159+
}
160+
});
161+
139162
$('.search-view-type-list').on('click', function(e) {
140163
e.preventDefault();
141164
Search.setViewType('list');

config/module.config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
'facetLabel' => View\Helper\FacetLabel::class,
3939
'facetLink' => View\Helper\FacetLink::class,
4040
'facetLinks' => View\Helper\FacetLinks::class,
41+
'facetSelect' => View\Helper\FacetSelect::class,
4142
'formNote' => View\Helper\FormNote::class,
4243
'hiddenInputsFromFilteredQuery' => View\Helper\HiddenInputsFromFilteredQuery::class,
4344
'searchForm' => View\Helper\SearchForm::class,

src/Form/Admin/SearchConfigConfigureForm.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,12 +445,13 @@ public function init(): void
445445
'type' => DataTextarea::class,
446446
'options' => [
447447
'label' => 'List of facets', // @translate
448-
'info' => 'List of facets that will be displayed in the search page. Format is "field = Label".', // @translate
448+
'info' => 'List of facets that will be displayed in the search page. Format is "field = Label" and optionnally " = Select".', // @translate
449449
'as_key_value' => true,
450450
'key_value_separator' => '=',
451451
'data_keys' => [
452452
'name',
453453
'label',
454+
'type',
454455
],
455456
],
456457
'attributes' => [

src/View/Helper/FacetSelect.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace AdvancedSearch\View\Helper;
4+
5+
class FacetSelect extends AbstractFacet
6+
{
7+
protected $partial = 'search/facet-select';
8+
}

view/search/facet-select.phtml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* @var \Laminas\View\Renderer\PhpRenderer $this
4+
* @var string $name
5+
* @var array $facetValues
6+
* @var array $options Facets options.
7+
*/
8+
9+
$plugins = $this->getHelperPluginManager();
10+
$translate = $plugins->get('translate');
11+
$escape = $plugins->get('escapeHtml');
12+
$escapeAttr = $plugins->get('escapeHtmlAttr');
13+
14+
$isFacetModeDirect = ($options['mode'] ?? '') === 'link';
15+
$displayCount = !empty($options['display_count']);
16+
?>
17+
18+
<select name="facet[<?= $name ?>][]" multiple="multiple" class="search-facet-items chosen-select" data-placeholder="<?= $translate('Select below…') ?>">
19+
<?php foreach ($facetValues as $facetValue): ?>
20+
<option value="<?= $escapeAttr($facetValue['value']) ?>"<?= $isFacetModeDirect ? ' data-url="' . $escapeAttr($facetValue['url']) . '"' : '' ?><?= $facetValue['active'] ? ' selected="selected"' : '' ?>>
21+
<?= $escape($facetValue['value']) ?><?php if ($displayCount): ?> (<span class="count"><?= $facetValue['count'] ?>)<?php endif; ?>
22+
</option>
23+
<?php endforeach; ?>
24+
</select>

view/search/facets.phtml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ $facetActive = $plugins->get('facetActive');
2929
$facetLabel = $plugins->get('facetLabel');
3030
// Facet checkbox can be used in any case anyway, the js checks it.
3131
$facetElements = $isFacetModeButton ? $plugins->get('facetCheckboxes') : $plugins->get('facetLinks');
32+
$facetSelect = $plugins->get('facetSelect');
3233

3334
// Don't display facets for item sets when browsing an item set.
3435
if (!empty($itemSet)) {
@@ -71,7 +72,11 @@ $options = $searchPage->setting('facet', []);
7172
<?php foreach ($facets as $name => $facetValues): ?>
7273
<li class="search-facet">
7374
<h4><?= $translate($facetLabel($name)) ?></h4>
75+
<?php if (($options['facets'][$name]['type'] ?? 'Checkbox') === 'Select'): ?>
76+
<?= $facetSelect($name, $facetValues, $options) ?>
77+
<?php else: ?>
7478
<?= $facetElements($name, $facetValues, $options) ?>
79+
<?php endif; ?>
7580
</li>
7681
<?php endforeach; ?>
7782
</ul>

0 commit comments

Comments
 (0)