Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/search fixes #187

Merged
merged 5 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 21 additions & 57 deletions config/sync/views.view.os2loop_search_db.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,10 @@ display:
os2loop_post: search_result
os2loop_question: search_result
fields:
os2loop_page_content:
search_api_rendered_item:
id: search_api_rendered_item
table: search_api_index_os2loop_search_db_index
field: os2loop_page_content
id: os2loop_page_content
entity_type: null
entity_field: null
plugin_id: search_api_field
field: search_api_rendered_item
relationship: none
group_type: group
admin_label: ''
Expand Down Expand Up @@ -123,34 +120,25 @@ display:
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_label_colon: false
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
click_sort_column: value
type: text_default
settings: { }
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
field_rendering: true
fallback_handler: search_api
fallback_options:
link_to_item: false
use_highlighting: false
multi_type: separator
multi_separator: ', '
view_modes:
'entity:comment':
os2loop_post_comment: search_result
os2loop_question_answer: search_result
'entity:node':
os2loop_documents_collection: search_result
os2loop_documents_document: search_result
os2loop_external: search_result
os2loop_post: search_result
os2loop_question: search_result
plugin_id: search_api_rendered_item
filters:
search_api_fulltext:
id: search_api_fulltext
Expand Down Expand Up @@ -219,18 +207,6 @@ display:
expose:
label: Relevance
plugin_id: search_api
changed:
id: changed
table: search_api_index_os2loop_search_db_index
field: changed
relationship: none
group_type: group
admin_label: ''
order: ASC
exposed: true
expose:
label: Changed
plugin_id: search_api
title:
id: title
table: search_api_index_os2loop_search_db_index
Expand All @@ -243,29 +219,17 @@ display:
expose:
label: Title
plugin_id: search_api
changed_1:
id: changed_1
table: search_api_index_os2loop_search_db_index
field: changed
relationship: none
group_type: group
admin_label: 'Changed (fallback)'
order: DESC
exposed: false
expose:
label: ''
plugin_id: search_api
title_1:
id: title_1
created:
id: created
table: search_api_index_os2loop_search_db_index
field: title
field: created
relationship: none
group_type: group
admin_label: 'Title (fallback)'
admin_label: ''
order: ASC
exposed: false
exposed: true
expose:
label: ''
label: 'Authored on'
plugin_id: search_api
title: Search
header:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Drupal\search_api_autocomplete\Suggestion\Suggestion;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\Url;

/**
* Search Api Autocomplete Helper.
Expand Down Expand Up @@ -193,6 +194,7 @@ public function formAlter(array &$form, FormStateInterface $form_state, $form_id
// Add facet filter query to form to keep the filters when submitting
// search form.
$request = $this->requestStack->getCurrentRequest();
$parameters = $request->query->all();
$facetFilterName = 'f';
$facetFilters = $request->get($facetFilterName);
if (!empty($facetFilters) && is_array($facetFilters)) {
Expand All @@ -206,6 +208,57 @@ public function formAlter(array &$form, FormStateInterface $form_state, $form_id
];
}
}

// Create links for sorting.
$sortLinks = [
'sortDefault' => [
'label' => $this->t('Best match'),
'requestAlters' => [
'sort_by' => 'search_api_relevance',
'sort_order' => 'DESC',
],
],
'sortNewest' => [
'label' => $this->t('Newest first'),
'requestAlters' => [
'sort_by' => 'created',
'sort_order' => 'DESC',
],
],
'sortOldest' => [
'label' => $this->t('Oldest first'),
'requestAlters' => [
'sort_by' => 'created',
'sort_order' => 'ASC',
],
],
'sortAlphabetic' => [
'label' => $this->t('Alphabetic'),
'requestAlters' => [
'sort_by' => 'title',
'sort_order' => 'ASC',
],
],
];

$form['#sortLinks'] = [];
foreach ($sortLinks as $type => $values) {
if (empty($parameters)) {
// Set default active.
$form['#sortLinks']['sortDefault']['active'] = TRUE;
}
else {
if (empty(array_diff_assoc($values['requestAlters'], $parameters))) {
$form['#sortLinks'][$type]['active'] = TRUE;
}
else {
$form['#sortLinks'][$type]['active'] = FALSE;
}
}
$newRequest = array_merge($parameters, $values['requestAlters']);
$form['#sortLinks'][$type]['url'] = Url::fromRoute('<current>', $newRequest)->toString();
$form['#sortLinks'][$type]['label'] = $values['label'];
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ $search-icon: bg-icon($search-icon-bg, $icon-color);
display: none;
}

.search-sort-btn.active {
text-decoration: underline;
}

.form-autocomplete {
@extend .form-control-lg;
border-radius: 1em;
Expand Down Expand Up @@ -225,6 +229,11 @@ input[type="search"]::-webkit-search-results-decoration {
}
}

// Hide empty facets.
.facet-empty.facet-hidden {
display: none;
}

.block-facet--checkbox {
.dropdown-menu {
min-width: 200px;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
* @ingroup themeable
*/
#}
<div class="row search-filters-collapse show">
<div class="col mx-4 pt-3 bg-light">
<label class="font-weight-bold">{{ label }}</label>
<div{{ attributes }}>

<div{{ attributes }}>
<div class="row search-filters-collapse show">
<div class="col mx-4 pt-3 bg-light">
<label class="font-weight-bold">{{ label }}</label>
{% block content %}
{{ content }}
{% endblock %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,26 @@
<div class="row">
{{ children }}
</div>
<div class="col-12">
<div class="row">
<div class="col ml-2 border-bottom">
<h6 class="d-inline align-middle p-0">{{ 'Search filters'|t }}</h6>
</div>
<div class="mr-2 border-bottom">
<button class="btn btn-sm btn-dark mb-2" type="button" data-toggle="collapse" data-target=".search-filters-collapse" aria-expanded="true"
aria-controls="searchFilterCollapseFacets searchFilterCollapseOrder searchFilterCollapseSortBy">
<span class="less">{{ 'Hide'|t }}</span><span class="more">{{ 'Show'|t }}</span> {{ 'filters'|t }}
</button>
</div>
</div>
</div>
<div class="col-12">
<div class="row justify-content-end">
<div class="btn-group my-2 mx-2">
{% for sort in element['#sortLinks'] %}
<a class="search-sort-btn btn btn-dark {{ sort.active ? 'active' }}" href="{{ sort.url }}">{{ sort.label }}</a>
{% endfor %}
</div>
</div>
</div>
</form>
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,4 @@
</div>
{% endif %}
</div>
<div class="col-12">
<div class="row">
<div class="col ml-4 border-bottom">
<h6 class="d-inline align-middle p-0">{{ 'Search filters'|t }}</h6>
</div>
<div class="col-auto mr-4 border-bottom">
<button class="btn btn-sm btn-dark mb-2" type="button" data-toggle="collapse" data-target=".search-filters-collapse" aria-expanded="true"
aria-controls="searchFilterCollapseFacets searchFilterCollapseOrder searchFilterCollapseSortBy">
<span class="less">{{ 'Hide'|t }}</span><span class="more">{{ 'Show'|t }}</span> {{ 'filters'|t }}
</button>
</div>
</div>
</div>

Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
*/
#}
{% set classes = [
'col bg-light ml-4 pt-2 collapse show search-filters-collapse',
'col bg-light ml-4 pt-2 d-none',
'form-item',
'js-form-item',
'form-type-' ~ type|clean_class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
*/
#}
{% set classes = [
'col bg-light mr-4 pt-2 collapse show search-filters-collapse',
'col bg-light mr-4 pt-2 d-none',
'form-item',
'js-form-item',
'form-type-' ~ type|clean_class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% set os2loop_settings = get_os2loop_config('os2loop_settings.settings') %}

<div class="col-12">
<div class="mb-4">
<h2 class="h5">{{ 'Unfortunately your search yielded no result.'|t }}</h2>
<div class="d-block">{{ 'Check that you have spelled correctly or try another search term.'|t }}</div>
</div>
{% if os2loop_settings.node_type.os2loop_question %}
<div class="w-100 py-2 border-top"></div>
<div class="mb-4">
<h2 class="h5">{{ 'Do you need answers to a question?'|t }}</h2>
<div>{{ 'You can create a new question by pressing the button below.'|t }}</div>
</div>
<div class="mb-4">
<a href="{{ path('node.add', {'node_type': 'os2loop_question'}) }}" class="btn btn-secondary">{{ 'Create new question'|t }}</a>
</div>
{% endif %}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{#
/**
* @file
* Default theme implementation for main view template.
*
* Available variables:
* - attributes: Remaining HTML attributes for the element.
* - css_name: A CSS-safe version of the view name.
* - css_class: The user-specified classes names, if any.
* - header: The optional header.
* - footer: The optional footer.
* - rows: The results of the view query, if any.
* - empty: The content to display if there are no rows.
* - pager: The optional pager next/prev links to display.
* - exposed: Exposed widget form/info to display.
* - feed_icons: Optional feed icons to display.
* - more: An optional link to the next page of results.
* - title: Title of the view, only used when displaying in the admin preview.
* - title_prefix: Additional output populated by modules, intended to be
* displayed in front of the view title.
* - title_suffix: Additional output populated by modules, intended to be
* displayed after the view title.
* - attachment_before: An optional attachment view to be displayed before the
* view content.
* - attachment_after: An optional attachment view to be displayed after the
* view content.
* - dom_id: Unique id for every view being printed to give unique class for
* JavaScript.
*
* @see template_preprocess_views_view()
*
* @ingroup themeable
*/
#}
{% set classes = [
dom_id ? 'js-view-dom-id-' ~ dom_id,
] %}
<div{{ attributes.addClass(classes) }}>
{{ title_prefix }}
{{ title }}
{{ title_suffix }}

{% if header %}
<header>
{{ header }}
</header>
{% endif %}

{{ exposed }}
{{ attachment_before }}

{% if rows -%}
{{ rows }}
{% elseif empty -%}
{% include '@os2loop_theme/views/no-result-views-os2loop-search-db.html.twig' %}
{% endif %}
{{ pager }}

{{ attachment_after }}
{{ more }}

{% if footer %}
<footer>
{{ footer }}
</footer>
{% endif %}
</div>