22 February 2013

Search API server in settings.php

Shy Carrot

Sometimes, instead of using features, you need to be able define server settings in settings.php. This is especially useful when you do a lot of sql syncing as you only need to do a cache clear to start using the server settings defined in the file thus eliminating the need for feature reverts.



So without further due here is a quick snippet which will use settings.php $conf variables for Search API Solr server:



/**
* Implements hook_default_search_api_server().
*/
function yourmodule_default_search_api_server() {
 $items = array();

 $host = variable_get('solr_search_api_host', 'localhost');
 $port = variable_get('solr_search_api_port', '8983');
 $path = variable_get('solr_search_api_path', '\\/sorl');

 $items['solr'] = entity_import('search_api_server', '{
   "name" : "Solr server",
   "machine_name" : "solr",
   "description" : "",
   "class" : "search_api_solr_service",
   "options" : {
     "host" : "' . $host . '",
     "port" : "' . $port . '",
     "path" : "' . $path . '",
     "http_user" : "",
     "http_pass" : "",
     "excerpt" : 1,
     "retrieve_data" : 1,
     "highlight_data" : 1
   },
   "enabled" : "1",
   "rdf_mapping" : []
 }');
 
 return $items;
}


Then add the following in settings.php:



$conf['solr_search_api_host'] = 'https://my.search.server';
$conf['solr_search_api_port'] = '8080';
$conf['solr_search_api_path'] = '\\/sorl';


...and you're done :)