The Search API Solr module for Drupal is a nice way to use Solr as the server for the Search API module. Its pretty flexible with customizing the data that you want to return from the server but what if you need something that’s not directly available from the Solr server? What if you also need the data to be available to the client side and do some kind of data manipulation from there?
In this article I’ll be showing you how to do just that. We will be creating a custom module that will tap into the API of the Search API Solr Module to modify the default results that are returned and make it available to the client side.
Let’s begin by creating the
.info file for our custom module:
1 2 3 4 5 6
Before we dive into the ‘how’ let me tell you about a sample use case first. For example, we have the following data stored in the Solr server:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
But we want to display the location of the business in Google Maps by using the postal code as the query. Normally we would want to actually update the data that’s in the Solr server so there will be no need to modify the results from it. But what if we don’t have access to the Solr server? What if its a remote server run by someone else? For that we will need to actually modify the results as they are returned.
Now were ready to create the
.module file which will contain the code that will tap into the search api solr module. The search api solr search module already provides a hook that we can tap into to modify the search results. It’s called the
search_api_solr_search_results_alter(). This takes up 3 arguments, the first one is the
$results which contains the current result set. As you can see its an argument that’s passed by reference so we don’t really have to return it. The second is the
$query, we won’t really use it in our custom module but its the variable that contains the current query. The third argument is the
$response, this is the response object returned from Solr.
In the code below we first have to check whether we are in the specific view in which we want to execute our script.
In this case were checking if we are in the
Next we declare a variable called
$locations, this will store the modified version of the results returned from the Solr server.
Next we loop through the results and get the business name and the postal code.
We then use the postal code to query the Google Geocoding API.
After that, we simply convert the results returned from the Google Geocoding API to an array so we could extract
the coordinates (latitude and longitude). The coordinates is what we ultimately need in order to display the location of the businesses in a Google Map.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
Accessing the data from the client-side
The modified results is now available from the client side via the
You can make use of this data however you want.