diff --git README.txt README.txt index a407aca..f09ab60 100644 --- README.txt +++ README.txt @@ -174,7 +174,23 @@ hook_apachesolr_modify_query(&$query, &$params, $caller); // I only want to see articles by the admin! $query->add_filter("uid", 1); } - + +hook_apachesolr_prepare_query(&$query, &$params, $caller); + + This is pretty much the same as hook_apachesolr_modify_query() but runs earlier + and before the query is statically cached. It can e.g. be used to add + available sorts to the query. + + Example: + + function my_module_apachesolr_prepare_query(&$query) { + // Add a sort on the node ID. + $query->set_available_sort('nid', array( + 'title' => t('Node ID'), + 'default' => 'asc', + )); + } + hook_apachesolr_cck_fields_alter(&$mappings) Add or alter index mappings for CCK types. The default mappings array handles just diff --git Solr_Base_Query.php Solr_Base_Query.php index c555840..810474b 100644 --- Solr_Base_Query.php +++ Solr_Base_Query.php @@ -128,7 +128,8 @@ class Solr_Base_Query implements Drupal_Solr_Query_Interface { $this->filterstring = trim($filterstring); $this->parse_filters(); $this->available_sorts = $this->default_sorts(); - $this->parse_sortstring($sortstring); + $this->sortstring = trim($sortstring); + $this->parse_sortstring(); $this->base_path = $base_path; $this->id = ++self::$idCount; } @@ -236,9 +237,9 @@ class Solr_Base_Query implements Drupal_Solr_Query_Interface { $this->subqueries = array(); } - protected function parse_sortstring($sortstring) { + protected function parse_sortstring() { // Substitute any field aliases with real field names. - $sortstring = strtr(trim($sortstring), array_flip($this->field_map)); + $sortstring = strtr($this->sortstring, array_flip($this->field_map)); // Score is a special case - it's the default sort for Solr. if ('' == $sortstring) { $this->set_solrsort('score', 'asc'); @@ -270,10 +271,14 @@ class Solr_Base_Query implements Drupal_Solr_Query_Interface { public function set_available_sort($name, $sort) { // We expect non-aliased sorts to be added. $this->available_sorts[$name] = $sort; + // Re-parse the sortstring. + $this->parse_sortstring(); } public function remove_available_sort($name) { unset($this->available_sorts[$name]); + // Re-parse the sortstring. + $this->parse_sortstring(); } /** diff --git apachesolr_search.module apachesolr_search.module index 6700de8..d433e89 100644 --- apachesolr_search.module +++ apachesolr_search.module @@ -207,6 +207,12 @@ function apachesolr_search_execute($keys, $filters, $solrsort, $base_path = '', apachesolr_search_add_facet_params($params, $query); apachesolr_search_add_boost_params($params, $query, $solr); + // Allow modules to alter the query prior to statically caching it. + // This can e.g. be used to add available sorts. + foreach (module_implements('apachesolr_prepare_query') as $module) { + $function_name = $module . '_apachesolr_prepare_query'; + $function_name($query, $params, $caller); + } // Cache the built query. Since all the built queries go through // this process, all the hook_invocations will happen later