Change Default Search Query in a Theme?

Hi Omeka community,

This may be a noobie question, but I have looked high and low for the answer, so it may be usepful to have this in the forum.

I have a site with thousands of items that refer to street addresses. On my resource template I have a special field for sort order which is the street name concatenated with a zero padded house number, so that 3 Cambridge street comes before 1000 Cambridge St.

StreetSort
Cambridge_St_0001000
Cambridge_St_0000003

Theoretically, I could change the default search query to order by this field. This is the sort order that makes most sense.

My questions:

  1. What are the files and values I should be looking to adjust to accomplish this?
  2. Is this something I can do in a Theme?
  3. Or is it necessary to write a module to over-ride the default search query?
  4. Or (for a php challenged hacker) can this be accomplished with javascript calls to the Omeka-S API.

Thank you!

Demo site: Cambridge Digital Architecture Survey and History.

You’re looking to change the sorting for this one what page? Just the browse pages?

One common solution to this is to use the Navigation config to do it, as you can add parameters to the Browse page entries there, which let you set sorting. You look like you’re not really doing browses in the navigation so this might not be the route for you. Similarly sorting can be set in the Browse preview blocks which it looks like you’re using in a few places, and also in any manual links to browse pages that you might be using.

These all involve just putting in the query string for the sorting into the URL. To change the default that’s used when there’s no query string, that currently involves a module. We have an upcoming feature in the pipeline to allow changing default sorting just through the admin interface, though.

Thank you John.
Sorry my question was not clear. What I would like to find is the location where I can change the query that is used by the Omeka-S search tool. If I could change the sort order in this query, that would be great. But it would also be great if I could cause it only to search on items having a particular resource template – or other sorts of filters.

After some more looking around. I now understand that SearchController.php is involved. I gather from the page on Theme Modifications That themes don’t override controllers (like what you said.) I’ll try making module that hacks SearchController.php. Any other hints you can provide are welcome.

I’m glad to hear that you are working on configurations for the search interfaces. I’ll take a look at the AdvanceSearch module, too.

Enjoy the weather.
–pbc

Thanks again.

Or maybe I’ll try the AdvancedSearch plugin.

–pbc

I figured out that it is possible to change the default search query including the query expression and order-by settings in a theme without creating a new module.

  1. Copy: omeka-s/applicastion/view/common/search-form.phtml
    to: omeka-s/themes/yourtheme/view/common

  2. Edit the html form to add hidden fields that define a search query. The default query is ‘fulltext_search’ here is my form statement that changes the search expression to “title contains” and the default search order to my custom property: cdash:maplot.

<?php
 $translate = $this->plugin('translate');
// A lot of lines are removed here since we know in advance that our custom search  returns only items.

//  Notice that the controller and action here have been modified below to go directly to the Item/Browse 
// action.  Since our search returns only items we can simplify the route.
$searchAction = $this->url('site/resource', ['controller' => 'item', 'action' => 'browse'], true);

// This line captures the user's search string if it already exists in the url parameters, so that it can 
// show up in the text box.

$searchValue = $this->escapeHtml($this->params()->fromQuery('property[0][text]', ''));
?>

<!-- Here we build a query predominantly of hard-coded values in hidden form fields. -->
<form action="<?php echo $this->escapeHtml($searchAction); ?>" id="search-form">
    <input type="text" name="property[0][text]" value="<?php echo $searchValue; ?>"
        placeholder="<?php echo $translate('Title Contains'); ?>"
        aria-label="<?php echo $translate('Title Contains'); ?>">

     <!-- Make sure that the Sort_By field is listed  in th Item Browse -->
     <input type="hidden"  name="sort_by" data-name="sort_by" value="cdash:mapLot">
     <input type="hidden"  name="sort_order" data-name="sort_order" value="asc">
     <input type="hidden"  name='property[0][joiner]' value='and' >
     <input type="hidden"  name='property[0][property]' value='1' >
     <input type="hidden"  name='property[0][type]' value='in' >
     <input type="hidden"  name='resource_template_id[]' value='7'>

     <button type="submit"><?php echo $translate('CDASHSearch'); ?></button>
</form>

If your order-by property is not included in the drop-down menu on the search page, you will need to copy: omeka-s/application/view/omeka/site/item/browse.phtml
to: omeka-s/themes/yourtheme/omeka/site/item/browse.phtml
and edit the SortHeadings.

Handy tools and references:
Omeka-S ApI Query documentation
Use Advanced search to look at how searches translate to URL parameters.
Use a URL decoder to translate the query parameters to readable text.
Keep playing with your search-form.phtml until the URL produced and the search behavior are what you want.

I hope this helps somebody.

–pbc (junior novice php/laminas/omeka-s hacker)

2 Likes