diff --git a/modules/quant_search/css/quant_search.admin.css b/modules/quant_search/css/quant_search.admin.css new file mode 100644 index 00000000..4c44f052 --- /dev/null +++ b/modules/quant_search/css/quant_search.admin.css @@ -0,0 +1,33 @@ +.quant-search-page-edit-form #edit-facets td { + width: 20%; +} + +.quant-search-page-edit-form #edit-facets td input[type=submit] { + min-width: 155px; +} + +.quant-search-page-edit-form #edit-facets .facet-remove { + margin-top: 2.25rem; +} + +.quant-search-page-edit-form #edit-facets .facet-add { + display: block; +} + +@media (max-width: 1100px) { + + .quant-search-page-edit-form #edit-facets td { + display: block; + width: auto; + height: auto; + margin-left: 1rem; + } + + .quant-search-page-edit-form #edit-facets td.tabledrag-cell { + margin-left: auto; + } + + .quant-search-page-edit-form #edit-facets td.tabledrag-cell div { + width: calc(100% + .25rem); + } +} diff --git a/modules/quant_search/css/quant-search.css b/modules/quant_search/css/quant_search.algolia.css similarity index 100% rename from modules/quant_search/css/quant-search.css rename to modules/quant_search/css/quant_search.algolia.css diff --git a/modules/quant_search/js/quant-search.js b/modules/quant_search/js/quant-search.js index 1de6dcbf..7f7f531f 100644 --- a/modules/quant_search/js/quant-search.js +++ b/modules/quant_search/js/quant-search.js @@ -70,7 +70,8 @@ ]); } - if (drupalSettings.quantSearch.facets.length > 0) { + var facets = drupalSettings.quantSearch.facets; + if (typeof facets === 'object' && facets !== null && !Array.isArray(facets)) { if (drupalSettings.quantSearch.display.results.show_clear_refinements) { search.addWidgets([ instantsearch.widgets.clearRefinements({ @@ -78,8 +79,8 @@ }), ]); } - for (var facet_key in drupalSettings.quantSearch.facets) { - const facet = drupalSettings.quantSearch.facets[facet_key]; + for (var facet_key in facets) { + const facet = facets[facet_key]; switch (facet.facet_display) { case "checkbox": diff --git a/modules/quant_search/quant_search.install b/modules/quant_search/quant_search.install new file mode 100644 index 00000000..0701470e --- /dev/null +++ b/modules/quant_search/quant_search.install @@ -0,0 +1,33 @@ +getStorage('quant_search_page'); + $ids = \Drupal::entityQuery('quant_search_page')->execute(); + $pages = $storage->loadMultiple($ids); + + // Combine facet_type configuration fields for better tabledrag UX. + foreach ($pages as $page) { + $facets = $page->get('facets'); + $fields = ['facet_type', 'custom_key', 'taxonomy_vocabulary']; + foreach ($facets as $i => $facet) { + foreach ($fields as $field) { + if (isset($facet[$field])) { + $facets[$i]['facet_type_config'][$field] = $facet[$field]; + unset($facets[$i][$field]); + } + } + $page->set('facets', $facets); + } + $page->save(); + } + +} diff --git a/modules/quant_search/quant_search.libraries.yml b/modules/quant_search/quant_search.libraries.yml index b0b9229a..f8623a51 100644 --- a/modules/quant_search/quant_search.libraries.yml +++ b/modules/quant_search/quant_search.libraries.yml @@ -1,8 +1,13 @@ +drupal.quant_search.admin: + css: + theme: + css/quant_search.admin.css: {} + algolia: css: theme: https://cdn.jsdelivr.net/npm/instantsearch.css@7/themes/algolia-min.css: { type: external, minified: true } - css/quant-search.css: {} + css/quant_search.algolia.css: {} js: https://cdn.jsdelivr.net/npm/algoliasearch@4/dist/algoliasearch-lite.umd.js: { type: external, minified: true } https://cdn.jsdelivr.net/npm/instantsearch.js@4: { type: external, minified: true } diff --git a/modules/quant_search/src/Controller/Search.php b/modules/quant_search/src/Controller/Search.php index 0c85524a..69b430da 100644 --- a/modules/quant_search/src/Controller/Search.php +++ b/modules/quant_search/src/Controller/Search.php @@ -323,9 +323,9 @@ public static function processTranslatedFacetKeys(array $facets) { foreach ($facets as $k => $facet) { $lang = $facet['facet_language']; - switch ($facet['facet_type']) { + switch ($facet['facet_type_config']['facet_type']) { case "taxonomy": - $key = $facet['taxonomy_vocabulary'] . '_' . $lang; + $key = $facet['facet_type_config']['taxonomy_vocabulary'] . '_' . $lang; $containerKey = $key . "_{$k}"; $facet['facet_key'] = $key; $facet['facet_container'] = $containerKey; @@ -349,7 +349,7 @@ public static function processTranslatedFacetKeys(array $facets) { break; case "custom": - // @todo This + // @todo Allow custom entity key. break; default: diff --git a/modules/quant_search/src/Form/QuantSearchPageForm.php b/modules/quant_search/src/Form/QuantSearchPageForm.php index b0b22567..42c8a790 100644 --- a/modules/quant_search/src/Form/QuantSearchPageForm.php +++ b/modules/quant_search/src/Form/QuantSearchPageForm.php @@ -39,8 +39,11 @@ public static function create(ContainerInterface $container) { * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { + $form = parent::form($form, $form_state); + $form['#attached']['library'][] = 'quant_search/drupal.quant_search.admin'; + $page = $this->entity; $form['#tree'] = TRUE; @@ -217,10 +220,24 @@ public function form(array $form, FormStateInterface $form_state) { '#default_value' => $existingDisplayConfig['pagination']['per_page'] ?? 20, ]; - // Facet configuration. + // Create tabledrag facets table. $form['facets'] = [ - '#type' => 'fieldset', - '#title' => $this->t('Facets'), + '#type' => 'table', + // Do not show the weight header as each select has a label. + '#header' => [ + [ + 'data' => $this->t('Facet configuration'), + // IMPORTANT: Must be the correct value or tabledrag doesn't work! + 'colspan' => 6, + ], + ], + '#tabledrag' => [ + [ + 'action' => 'order', + 'relationship' => 'sibling', + 'group' => 'facet-sort-weight', + ], + ], '#prefix' => '