Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 into 1.1.0
  • Loading branch information
DiegoPino committed Jun 27, 2023
2 parents 9097564 + 715eb8f commit 4e3f689
Show file tree
Hide file tree
Showing 9 changed files with 300 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function build(FacetsSummaryInterface $facets_summary, array $build, arra
$exposed_input = $view->getExposedInput();
$view->getRequest()->getRequestUri();
$keys_to_filter = [];
$key_with_search_value = [] ;
$key_with_search_value = [];
// Oh gosh, blocks...
if (!$view->getDisplay()->isDefaultDisplay() && empty($view->getDisplay()->options['filters'] ?? [] )) {
$filters = $view->getDisplay()->handlers['filter'];
Expand All @@ -147,9 +147,17 @@ public function build(FacetsSummaryInterface $facets_summary, array $build, arra
elseif ($filter->getPluginId() == 'sbf_advanced_search_api_fulltext'
&& $filter->isExposed()
) {
$current_count = 1;
if ($filter->options['expose']['identifier'] ?? NULL ) {
$field_count_field = $filter->options['expose']['identifier'] . '_advanced_search_fields_count';
$current_count = $exposed_input[$field_count_field] ?? ($filter->options['expose']['advanced_search_fields_count'] ?? 1);
}
$extra_keys_to_filter = [];
$keys_to_filter[] =$filter->options['expose']['operator_id'] ?? NULL;
$keys_to_filter[] = $filter->options['expose']['operator_id'] ?? NULL;
$keys_to_filter[] = $filter->options['expose']['identifier'] ?? NULL;
if ($filter->options['expose']['identifier'] ?? NULL ) {
$keys_to_filter[] = $filter->options['expose']['identifier'] . '_advanced_search_fields_count';
}
$key_with_search_value[] = $filter->options['expose']['identifier'] ?? NULL;
$keys_to_filter[] = $filter->options['expose']['searched_fields_id'] ?? NULL;
$keys_to_filter[] = $filter->options['expose']['advanced_search_operator_id']
Expand All @@ -160,17 +168,20 @@ public function build(FacetsSummaryInterface $facets_summary, array $build, arra
$i < $filter->options['expose']['advanced_search_fields_count'] ?? 1;
$i++
) {

$extra_keys_to_filter[] = $key_to_filter . '_' . $i;
if (in_array($key_to_filter, $key_with_search_value)){
$key_with_search_value[] = $key_to_filter . '_' . $i;
if ($i < $current_count) {
$key_with_search_value[] = $key_to_filter . '_' . $i;
}
}
}
}
$keys_to_filter = array_filter($keys_to_filter);

$keys_to_filter = array_merge(
$keys_to_filter, $extra_keys_to_filter
);
$keys_to_filter = array_unique($keys_to_filter);
$keys_to_filter = array_filter($keys_to_filter);
}
}
}
Expand All @@ -187,9 +198,20 @@ public function build(FacetsSummaryInterface $facets_summary, array $build, arra
elseif ($filter['plugin_id'] == 'sbf_advanced_search_api_fulltext'
&& $filter['exposed']
) {
$current_count = 1;
if ($filter['expose']['identifier'] ?? NULL ) {
$field_count_field = $filter['expose']['identifier'] . '_advanced_search_fields_count';
$current_count = $exposed_input[$field_count_field] ?? ($filter['expose']['advanced_search_fields_count'] ?? 1);
}

$extra_keys_to_filter = [];
$keys_to_filter[] = $filter['expose']['operator_id'] ?? NULL;
$keys_to_filter[] = $filter['expose']['identifier'] ?? NULL;
// fields count = $filter['expose']['identifier']
if ($filter['expose']['identifier'] ?? NULL ) {
$keys_to_filter[]
= $filter['expose']['identifier'] . '_advanced_search_fields_count';
}
$key_with_search_value[] = $filter['expose']['identifier'] ??
NULL;
$keys_to_filter[] = $filter['expose']['searched_fields_id'] ??
Expand All @@ -203,17 +225,19 @@ public function build(FacetsSummaryInterface $facets_summary, array $build, arra
$i < $filter['expose']['advanced_search_fields_count'] ?? 1;
$i++
) {

$extra_keys_to_filter[] = $key_to_filter . '_' . $i;
if (in_array($key_to_filter, $key_with_search_value)) {
$key_with_search_value[] = $key_to_filter . '_' . $i;
if ($i < $current_count) {
$key_with_search_value[] = $key_to_filter . '_' . $i;
}
}
}
}
$keys_to_filter = array_filter($keys_to_filter);
$keys_to_filter = array_merge(
$keys_to_filter, $extra_keys_to_filter
);
$keys_to_filter = array_unique($keys_to_filter);
$keys_to_filter = array_filter($keys_to_filter);
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions modules/format_strawberryfield_views/css/advanced_search.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.adv-search-delone {
margin-right: 0.275rem;
}

.adv-search-addone {
margin-right: 0.275rem;
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,13 @@ view-ajax-interactions:
- core/views
- core/views.ajax
- format_strawberryfield/iiif_formatstrawberryfield_utils

advanced-search-default-submit:
js:
js/sbf-advanced-search-default-submit.js: {minified: false}
css:
component:
css/advanced_search.css: { }
dependencies:
- core/drupal
- core/jquery
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,17 @@ function format_strawberryfield_views_block_view_views_exposed_filter_block_sbf_
*/
function format_strawberryfield_views_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form_id == 'views_exposed_form') {
if (isset($form['actions']['submit'])) {
$form['actions']['#attached']['library'][] = 'format_strawberryfield_views/advanced-search-default-submit';
$form['actions']['submit']['#attributes']['autofocus'] = '';
$form['actions']['submit']['#attributes']['tabindex'] = 1;
$form['actions']['submit']['#attributes']['data-default-submit'] = '';
// Needed when multiple buttons are on the same form, by default the name is empty
// making \Drupal\Core\Form\FormBuilder::buttonWasClicked fail!
$form['actions']['submit']['#name'] = 'op';
}
foreach ($form as $key => $value) {
if (is_array($value) && isset ($value['#group']) && isset($value['#type']) && $value['#group'] == 'actions' && $value['#type'] == 'submit') {
if (is_array($value) && isset ($value['#group']) && isset($value['#type']) && $value['#group'] == 'actions' && ($value['#type'] == 'submit' || $value['#type'] == 'button' || $value['#type'] == 'link')) {
$form['actions'][$key] = $value;
unset($form[$key]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@
}
});
if (reloadfacets) {
console.log('reloading!');
Drupal.AjaxFacetsView.updateFacetsBlocks(href, options.extraData.view_name, options.extraData.view_display_id);
}
}
Expand All @@ -215,8 +214,6 @@
if (ajax_views_call && view_name && view_display_id) {
var exposed_form_selector = '#views-exposed-form-' + view_name.replace(/_/g, '-') + '-' + view_display_id.replace(/_/g, '-');
var $exposed_form = $(exposed_form_selector).length;
console.log($exposed_form);
console.log(exposed_form_selector);
if ($exposed_form > 0) {
$exposed_form = 1;
}
Expand Down Expand Up @@ -258,10 +255,7 @@
};

Drupal.AjaxCommands.prototype.SbfSetBrowserUrl = function (ajax, response) {
console.log(response.url);
window.history.replaceState(null, '', response.url);
}


})(jQuery, Drupal, once, drupalSettings);

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
(function ($, Drupal) {
Drupal.behaviors.sbfAdvancedSearchViewsForm = {
attach(context) {
var formInterception = function(defaultSubmitInput, ev) {
if (ev.keyCode == 13) {
ev.preventDefault();
ev.stopPropagation();
defaultSubmitInput.click();
}
};

var addMoreInterception = function(defaultSubmitInput, ev) {
ev.preventDefault();
ev.stopPropagation();
const $form = defaultSubmitInput.closest('.views-exposed-form');
let $count = $form.querySelector('input[name="sbf_advanced_search_api_fulltext_advanced_search_fields_count"]');
$count.value = Number($count.value) + 1;
defaultSubmitInput.click();
};

var delOneInterception = function(defaultSubmitInput, ev) {
ev.preventDefault();
ev.stopPropagation();
if (typeof ev.target.dataset.advancedSearchPrefix !== 'undefined') {
const $name = ev.target.dataset.advancedSearchPrefix + '_advanced_search_fields_count';
const $form = defaultSubmitInput.closest('.views-exposed-form');
let $count = $form.querySelector('input[name="'+$name+'"]');
if ($count) {
$count.value = Number($count.value) - 1;
const $tounsetSelector = ev.target.dataset.advancedSearchPrefix + '_' + $count.value;
let $tounset = $form.querySelector('input[name="' + $tounsetSelector + '"]');
if ($tounset) {
$tounset.value = '';
}
}
}
defaultSubmitInput.click();
};

for (const defaultSubmitInput of document.querySelectorAll('.views-exposed-form [data-default-submit]')) {
for (const formInput of defaultSubmitInput.form.querySelectorAll('input')) {
formInput.addEventListener('keypress', formInterception.bind(null, defaultSubmitInput));
}
for (const addMore of document.querySelectorAll('.views-exposed-form [data-advanced-search-addone]')) {
addMore.addEventListener('click', addMoreInterception.bind(null, defaultSubmitInput));
}
for (const delOne of document.querySelectorAll('.views-exposed-form [data-advanced-search-delone]')) {
delOne.addEventListener('click', delOneInterception.bind(null, defaultSubmitInput));
}
}
}
}
})(jQuery, Drupal);
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ public function ajaxView(Request $request) {

$response = new ViewAjaxResponse();



// Remove all of this stuff from the query of the request so it doesn't
// end up in pagers and tablesort URLs.
// @todo Remove this parsing once these are removed from the request in
Expand Down Expand Up @@ -172,19 +174,52 @@ public function ajaxView(Request $request) {
}
$view = $this->executableFactory->get($entity);
if ($view && $view->access($display_id) && $view->setDisplay($display_id) && $view->display_handler->ajaxEnabled()) {
$response->setView($view);

// Fix the current path for paging.
if (!empty($path)) {
$this->currentPath->setPath('/' . ltrim($path, '/'), $request);
}

// Because Exposed filters use Get and our form is submitting via POST
// We end with stale old values.

// Let's check if our view has our advanced search filter

$filters = $view->getDisplay($display_id)->display['display_options']['filters'] ?? [];
foreach ($filters as $filter) {
/* @var \Drupal\views\Plugin\views\ViewsHandlerInterface $filter */
if ($filter['plugin_id'] == 'sbf_advanced_search_api_fulltext'
&& $filter['exposed'] == TRUE
) {
if ($filter['expose']['identifier'] ?? NULL ) {
// At this stage the loaded View has already processed
// the whole GET bag as its own input based on the active request
// We need to alter that. Let's check if we have the id in both GET and POST
// If in POST, POST wins and replaces/needs to replace GET in the view
$views_post = $view->getRequest()->request->all();
unset($views_post['ajax_page_state']);
$views_get = $view->getRequest()->query->all();
if (isset($views_post[$filter['expose']['identifier']])) {
foreach ($views_get as $get_args_keys => $value) {
if (strpos($get_args_keys, $filter->options['expose']['identifier']) === 0) {
unset($views_get[$get_args_keys]);
}
}
}
}
$view->getRequest()->query->replace($views_post + $views_get);
}
}


// Add all POST data, because AJAX is always a post and many things,
// such as tablesorts, exposed filters and paging assume GET.
$request_all = $request->request->all();
unset($request_all['ajax_page_state']);
$query_all = $request->query->all();
$request->query->replace($request_all + $query_all);

$response->setView($view);
// Overwrite the destination.
// @see the redirect.destination service.
$origin_destination = $path;
Expand Down Expand Up @@ -236,7 +271,7 @@ public function ajaxView(Request $request) {
->merge($bubbleable_metadata)
->applyTo($exposed_form);
}
$response->addCommand(new ReplaceCommand("#views-exposed-form-" . $view_id, $this->renderer->render($exposed_form)));
$response->addCommand(new ReplaceCommand("#views-exposed-form-" . $view_id, $this->renderer->render($exposed_form)));
}

return $response;
Expand Down
Loading

0 comments on commit 4e3f689

Please sign in to comment.