Meta collection facets

To allow different sorting methods for different tabs, the Freemarker code linked in the Github example can be modified, like this:

<#--
  Generate an HTML drop-down for sorting results

  @param options Map of sort options, where keys are the `sort` paratmeter (e.g. `date`) and values the label (e.g. `Date (Newest first)`)
-->
<#macro SortDropdown>
<#-- People tab -->
  <#if question.inputParameterMap["f.Tabs|higher-education-people"]??>
    <#local options={
      "": "Alphabetical (last name)",
      "dmetapeopleLastName": "Reverse Alphabetical (last name)",
      "metapeopleFirstName": "Alphabetical (first name)",
      "dmetapeopleFirstName": "Reverse Alphabetical (first name)"} >
<#-- Programs/Courses tab -->
  <#elseif question.inputParameterMap["f.Tabs|higher-education-programs"]??>
    <#local options={
      "": "Relevance",
      "title": "Title (A-Z)",
      "dtitle": "Title (Z-A)"} >
<#-- Events tab -->
  <#elseif question.inputParameterMap["f.Tabs|higher-education-events"]??>
    <#local options={
        "": "Date (Oldest first)",
        "date": "Date (Newest first)",
        "title": "Title (A-Z)",
        "dtitle": "Title (Z-A)"} >
<#-- Facebook tab -->
  <#elseif question.inputParameterMap["f.Tabs|higher-education-facebook"]??>
    <#local options={
    "": "Date (Newest first)",
    "adate": "Date (Oldest first)",
    "title": "Title (A-Z)",
    "dtitle": "Title (Z-A)"} >
<#-- All other tabs -->
  <#else>
    <#local options={
      "": "Relevance",
      "date": "Date (Newest first)",
      "adate": "Date (Oldest first)",
      "title": "Title (A-Z)",
      "dtitle": "Title (Z-A)"} >
  </#if>

  <div class="dropdown float-right">
    <button class="btn btn-light btn-sm dropdown-toggle" id="search-sort" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      <#-- If there is no sort parameter, use "" which is the Relevance option -->
      <#-- If the sort parameter isn't in the options hash, use the default value for blank string  -->
      <span class="text-muted">Sort:</span> ${(options[question.inputParameterMap["sort"]!""])!options[""]}
    </button>
    <div class="dropdown-menu" aria-labelledby="search-sort">
      <#list options as key, value>
        <a class="dropdown-item" title="Sort by ${value}" href="${question.collection.configuration.value("ui.modern.search_link")}?${removeParam(QueryString, "sort")}&sort=${key}">${value}</a>
      </#list>
    </div>
  </div>
</#macro>

Note that the tab value looks like “f.Tabs|[collection-name]”. The easiest way to find the tab value is to click on it and look at the URL (the vertical bar will be HTML-encoded).

To default a sorting method for a specific tab, a “hook script” could be written. You can learn more about hook scripts here:

Here’s an example of a hook_pre_process.groovy script that default the sort to different ways depending on which tab is selected.

def q = transaction.question

// Sort by last name if the People tab is selected
if (q.inputParameterMap["f.Tabs|higher-education-people"] && !q.inputParameterMap["sort"]) {
    q.additionalParameters["sort"] = ["metapeopleLastName"]
}

// Sort by ascending date if the Events tab is selected
if (q.inputParameterMap["f.Tabs|higher-education-events"] && !q.inputParameterMap["sort"]) {
    q.additionalParameters["sort"] = ["adate"]
}

// Sort by date if the Social Media tab is selected
if (q.inputParameterMap["f.Tabs|higher-education-facebook"] && !q.inputParameterMap["sort"]) {
   q.additionalParameters["sort"] = ["date"]
}

// Apply sortall=true to achieve proper sorting for partially matching results
q.additionalParameters["sortall"] = ["true"]

The syntax for the sorting options (for example it may be prepended with ‘meta’ or ‘a’) can be found here:

1 Like