From 2e6307b0bb6a32c33b2da27e12c7c84ab0895f61 Mon Sep 17 00:00:00 2001 From: Rasmus Bech Date: Thu, 15 Nov 2018 09:32:03 +0100 Subject: [PATCH] Add sort order to facet attributes --- Block/Result.php | 19 ++++++++++++------- view/adminhtml/templates/facettitles.phtml | 6 +++++- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/Block/Result.php b/Block/Result.php index c356d0a..d64258d 100644 --- a/Block/Result.php +++ b/Block/Result.php @@ -49,17 +49,22 @@ public function getSpanAttributes() ]; if ($this->_scopeConfig->isSetFlag(Config::XML_PATH_FACETED_SEARCH_ENABLED)) { - if ($attributes = $this->_scopeConfig->getValue(Config::XML_PATH_FACETED_SEARCH_ATTRIBUTES)) { - $spanAttributes['data-facets-target'] = "#clerk-search-filters"; - $spanAttributes['data-facets-attributes'] = '["' . str_replace(',', '","', $attributes) . '"]'; + $spanAttributes['data-facets-target'] = "#clerk-search-filters"; + + if ($titles = $this->_scopeConfig->getValue(Config::XML_PATH_FACETED_SEARCH_TITLES)) { + $titles = json_decode($titles, true); + + // sort alphabetically by name + uasort($titles, function($a, $b) { + return $a['sort_order'] > $b['sort_order']; + }); + + $spanAttributes['data-facets-titles'] = json_encode(array_filter(array_combine(array_keys($titles), array_column($titles, 'label')))); + $spanAttributes['data-facets-attributes'] = json_encode(array_keys($titles)); if ($multiselectAttributes = $this->_scopeConfig->getValue(Config::XML_PATH_FACETED_SEARCH_MULTISELECT_ATTRIBUTES)) { $spanAttributes['data-facets-multiselect-attributes'] = '["' . str_replace(',', '","', $multiselectAttributes) . '"]'; } - - if ($titles = $this->_scopeConfig->getValue(Config::XML_PATH_FACETED_SEARCH_TITLES)) { - $spanAttributes['data-facets-titles'] = $titles; - } } } diff --git a/view/adminhtml/templates/facettitles.phtml b/view/adminhtml/templates/facettitles.phtml index 5241f7e..c3aa611 100644 --- a/view/adminhtml/templates/facettitles.phtml +++ b/view/adminhtml/templates/facettitles.phtml @@ -10,6 +10,7 @@ $values = json_decode($element->getValue(), true); getScopeLabel(); ?> + @@ -20,7 +21,10 @@ $values = json_decode($element->getValue(), true); - + + + +