From 87738bff330f14f90ea163584750059c7db93d5a Mon Sep 17 00:00:00 2001 From: Jere Ljungberg Date: Tue, 17 Dec 2024 11:19:47 +0200 Subject: [PATCH] UHF-11045: Fix autocomplete route --- .../modules/custom/helfi_etusivu/src/Form/NearYouForm.php | 7 +++++++ .../custom/hdbt_subtheme/dist/js/autocomplete.min.js | 2 +- .../custom/hdbt_subtheme/hdbt_subtheme.libraries.yml | 2 +- public/themes/custom/hdbt_subtheme/src/js/autocomplete.js | 3 ++- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/public/modules/custom/helfi_etusivu/src/Form/NearYouForm.php b/public/modules/custom/helfi_etusivu/src/Form/NearYouForm.php index ea445a4e..783f947b 100644 --- a/public/modules/custom/helfi_etusivu/src/Form/NearYouForm.php +++ b/public/modules/custom/helfi_etusivu/src/Form/NearYouForm.php @@ -6,6 +6,7 @@ use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Url; /** * Search form for near you page. @@ -30,6 +31,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#attached' => [ 'drupalSettings' => [ 'helsinki_near_you_form' => [ + 'autocompleteRoute' => Url::fromRoute('helfi_etusivu.helsinki_near_you_autocomplete')->tosTring(), 'minCharAssistiveHint' => $this->t('Type @count or more characters for results', [], ['context' => $translation_context]), 'inputAssistiveHint' => $this->t( 'When autocomplete results are available use up and down arrows to review and enter to select. Touch device users, explore by touch or with swipe gestures.', @@ -51,6 +53,11 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#placeholder' => $this->t('Eg. Vaasankatu 5', [], ['context' => 'Helsinki near you']), '#required' => TRUE, '#title' => $this->t('Address'), + '#label_attributes' => [ + 'class' => [ + 'hds-text-input__label', + ], + ], '#type' => 'helfi_etusivu_autocomplete', '#wrapper_attributes' => [ 'class' => [ diff --git a/public/themes/custom/hdbt_subtheme/dist/js/autocomplete.min.js b/public/themes/custom/hdbt_subtheme/dist/js/autocomplete.min.js index 13f8a25e..361209f0 100644 --- a/public/themes/custom/hdbt_subtheme/dist/js/autocomplete.min.js +++ b/public/themes/custom/hdbt_subtheme/dist/js/autocomplete.min.js @@ -1 +1 @@ -!function(){"use strict";((Drupal,t)=>{const e=t=>{if(!A11yAutocomplete)throw new Error("A11yAutocomplete object not found. Make sure the library is loaded.");if(!drupalSettings.helsinki_near_you_form)throw new Error("Helsinki near you form object not found. Configuration cannot be loaded for autocomplete.");const{noResultsAssistiveHint:e,someResultsAssistiveHint:s,oneResultAssistiveHint:i,highlightedAssistiveHint:o,inputAssistiveHint:n,minCharAssistiveHint:u}=drupalSettings.helsinki_near_you_form;A11yAutocomplete(t,{classes:{wrapper:"helfi-etusivu-autocomplete"},source:async(t,e)=>{if(t.length<3)return e([]);try{const s=await fetch(`/helsinki-near-you/results/autocomplete?q=${t}`,{});e(await s.json())}catch(t){console.error(t),e([])}},minChars:3,minCharAssistiveHint:u,inputAssistiveHint:n,noResultsAssistiveHint:e,someResultsAssistiveHint:s,oneResultAssistiveHint:i,highlightedAssistiveHint:o})};Drupal.behaviors.helfi_etusivu_autocomplete={attach:function(s,i){t("a11y_autocomplete_element","[data-helfi-etusivu-autocomplete]",s).forEach(e)}}})(Drupal,once)}(); \ No newline at end of file +!function(){"use strict";((Drupal,t)=>{const e=t=>{if(!A11yAutocomplete)throw new Error("A11yAutocomplete object not found. Make sure the library is loaded.");if(!drupalSettings.helsinki_near_you_form)throw new Error("Helsinki near you form object not found. Configuration cannot be loaded for autocomplete.");const{autocompleteRoute:e,noResultsAssistiveHint:i,someResultsAssistiveHint:s,oneResultAssistiveHint:o,highlightedAssistiveHint:n,inputAssistiveHint:u,minCharAssistiveHint:r}=drupalSettings.helsinki_near_you_form;A11yAutocomplete(t,{classes:{wrapper:"helfi-etusivu-autocomplete"},source:async(t,i)=>{if(t.length<3)return i([]);try{const s=await fetch(`${e}?q=${t}`,{});i(await s.json())}catch(t){console.error(t),i([])}},minChars:3,minCharAssistiveHint:r,inputAssistiveHint:u,noResultsAssistiveHint:i,someResultsAssistiveHint:s,oneResultAssistiveHint:o,highlightedAssistiveHint:n})};Drupal.behaviors.helfi_etusivu_autocomplete={attach:function(i,s){t("a11y_autocomplete_element","[data-helfi-etusivu-autocomplete]",i).forEach(e)}}})(Drupal,once)}(); \ No newline at end of file diff --git a/public/themes/custom/hdbt_subtheme/hdbt_subtheme.libraries.yml b/public/themes/custom/hdbt_subtheme/hdbt_subtheme.libraries.yml index cbcf283a..f11f5912 100644 --- a/public/themes/custom/hdbt_subtheme/hdbt_subtheme.libraries.yml +++ b/public/themes/custom/hdbt_subtheme/hdbt_subtheme.libraries.yml @@ -20,7 +20,7 @@ a11y_autocomplete: helfi_etusivu_autocomplete: js: dist/js/autocomplete.min.js: {} - version: 1.0.0 + version: 1.0.1 dependencies: - hdbt_subtheme/a11y_autocomplete - core/drupal diff --git a/public/themes/custom/hdbt_subtheme/src/js/autocomplete.js b/public/themes/custom/hdbt_subtheme/src/js/autocomplete.js index fd9b50d2..a591bba6 100644 --- a/public/themes/custom/hdbt_subtheme/src/js/autocomplete.js +++ b/public/themes/custom/hdbt_subtheme/src/js/autocomplete.js @@ -21,6 +21,7 @@ } const { + autocompleteRoute, noResultsAssistiveHint, someResultsAssistiveHint, oneResultAssistiveHint, @@ -39,7 +40,7 @@ } try { - const response = await fetch(`/helsinki-near-you/results/autocomplete?q=${searchTerm}`, {}); + const response = await fetch(`${autocompleteRoute}?q=${searchTerm}`, {}); const data = await response.json(); results(data); }