Rename/Reorder Apache Solr options in Drupal 6
There are two ways to rename the sorting methods available in the Drupal 6 Apache Solr module. First is the simpler method of changing the translation of the sort title as it is passed through t(). Second is changing the available sorts in the query object in a custom module. If you need to re-order, changing the query object is your only option. The default available sorts are listed in Solr_Base_Query.php:
/**
* Returns a default list of sorts.
*/
protected function default_sorts() {
// The array keys must always be real Solr index fields.
return array(
'score' => array('title' => t('Relevancy'), 'default' => 'desc'),
'sort_title' => array('title' => t('Title'), 'default' => 'asc'),
'type' => array('title' => t('Type'), 'default' => 'asc'),
'sort_name' => array('title' => t('Author'), 'default' => 'asc'),
'created' => array('title' => t('Date'), 'default' => 'desc'),
);
}
The functions available for modifying these default values or adding new ones are remove_available_sort($name) and set_available_sort($name, $sort). Both of which are accessible via the $query object in hook_apachesolr_prepare_query(&$query, &$params) and hook_apachesolr_modify_query(&$query, &$params). Note: prepare_query changes are visible to the user, and modify_query changes are not visible to the user. In this case, we don’t want the sort options to display to a user to so I will use prepare_query.
function module_apachesolr_prepare_query(&$query, &$params) { //Changes visible to user.
//Remove sorts
$query->remove_available_sort('type');
$query->remove_available_sort('sort_name');
//Rename/Reorder sorts
$query->remove_available_sort('score');
$query->set_available_sort('score', array('title' => t('Relevance'), 'default' => 'desc'));
$query->remove_available_sort('sort_title');
$query->set_available_sort('sort_title', array('title' => t('A to Z'), 'default' => 'asc'));
$query->remove_available_sort('created');
$query->set_available_sort('created', array('title' => t('Publication Date'), 'default' => 'desc'));
}
Comments
(Statically copied from previous site)
Andrea replied on June 2, 2011 - 6:14am
You saved me a lot of time with this - it was exactly what I needed to do. Thanks!!
Myles replied on January 5, 2012 - 11:54am
Thanks for simplifying the complex method for us. I tend to quickly lose my patience when it comes to painstakingly figuring out the ins and outs from the Drupal modules but now this has made it a lot more manageable to grasp. I am just hoping that I would get to execute it as precisely as possible.
Myles