diff --git a/search_api_override.info b/search_api_override.info index d07e04a..ddbd8fd 100644 --- a/search_api_override.info +++ b/search_api_override.info @@ -2,3 +2,10 @@ name = Search API Overrides description = Helper module to make search api server configuration possible in the settings.php core = 7.x package = Search + +; Information added by Drupal.org packaging script on 2014-02-25 +version = "7.x-1.0-rc1" +core = "7.x" +project = "search_api_override" +datestamp = "1393356206" + diff --git a/search_api_override.module b/search_api_override.module index dae4446..803377b 100644 --- a/search_api_override.module +++ b/search_api_override.module @@ -2,15 +2,15 @@ /** * @file - * Module file to hook into server definition, to alter the given settings. + * Module file to hook into server|index definition, to alter the given settings. * * Mode * ---- * * The module uses one of two modes: - * - load: the server configuration is overriden on every server load. This way + * - load: the server|index configuration is overriden on every load. This way * the settings cannot be overriden via the backend. - * - default: the server configuration is set via the default hook. This way the + * - default: the configuration is set via the default hook. This way the * configuration is set on inital import or a features revert for the given * feature. By choosing that option, you can override the settings in the * backend. @@ -24,18 +24,21 @@ * settings are never changed by and admin user, you should use the 'load' * method. That way, it could only be changed in code. * - * Server settings + * Server|Index settings * --------------- * * You can override specific settings by providing the overriding configuration - * by setting a config array in the settings.php: 'search_api_override_servers'. + * by setting a config array in the settings.php: 'search_api_override_servers' + * or 'search_api_override_indexes'. * - * Each array represents an override for one server. The definition is keyed by + * Each array represents an override for one server|index. The definition is keyed by * the machine name of the server. Ech definition may consist of the following * options - all are optional: - * - name: Admin title of the server - * - description: Admin description of the server - * - options: array of server specific configuration. Options will be merged + * - name: Admin title + * - description: Admin description + * - server: for indexes only; switch the server the index is using + * - read_only: for indexes only; whether or not to write to the index + * - options: array of specific configuration. Options will be merged * with the existing one. This way not all options have to be defined when * overring. E.g. in case of Solr, you could only override the path, but need * not explecitely set the host or port. @@ -54,6 +57,17 @@ * ), * ), * ); + * $conf['search_api_override_indexes'] = array( + * 'solr-index-id' => array( + * 'name' => 'Use Local Index (Overridden)', + * 'description' => 'An overridden description', + * 'index' => 'solr-server-alt-id', + * 'options' => array( + * // We do not have to set all values. + * 'cron_limit' => '100', + * ), + * ), + * ); * @endcode * Note: This is an example as solr configurations vary. * @@ -75,12 +89,27 @@ function search_api_override_default_search_api_server_alter(&$defaults) { } } +/** + * Implements hook_default_search_api_index_alter(). + */ +function search_api_override_default_search_api_index_alter(&$defaults) { + $settings = search_api_override_settings(); + + // Only change the settings this way if we use the 'default' mode. + if (!empty($settings['mode']) && $settings['mode'] == 'default') { + foreach ($defaults as $machine_name => &$index) { + if (!empty($settings['indexes'][$machine_name])) { + _search_api_override_apply_single_override($index, $settings['indexes'][$machine_name]); + } + } + } +} /** * Implements hook_search_api_server_load(). */ function search_api_override_search_api_server_load($servers) { - // Get the solr host overrides. + // Get the server overrides. $settings = search_api_override_settings(); // Only proceed if we are in "load" mode. @@ -95,30 +124,59 @@ function search_api_override_search_api_server_load($servers) { } /** - * Replaces the server configuration with the given override array. + * Implements hook_search_api_index_load(). + */ +function search_api_override_search_api_index_load($indexes) { + // Get the index overrides. + $settings = search_api_override_settings(); + + // Only proceed if we are in "load" mode. + if (!empty($settings['mode']) && $settings['mode'] == 'load') { + foreach ($indexes as &$index) { + // Check if we got an override setting for the given index. + if (!empty($settings['indexes'][$index->machine_name])) { + _search_api_override_apply_single_override($index, $settings['indexes'][$index->machine_name]); + } + } + } +} + +/** + * Replaces the server|index configuration with the given override array. * - * @param SearchApiServer $server - * The search api server object being overriden. + * @param SearchApiServer|SearchApiIndex $component + * The search api server or index object being overriden. * @param array $override - * The override configuration for the given server, it may contain those keys: + * The override configuration for the given server or index, it may contain those keys: * - name: Administrative title for the server * - description: Administrative description for the server + * - server: for index object; specify alternative server + * - read_only: for index object; whether or not to write to the index * - options: Server specific options */ -function _search_api_override_apply_single_override($server, $override) { +function _search_api_override_apply_single_override($component, $override) { // We can override the name, if it was explecitely set. if (isset($override['name'])) { - $server->name = $override['name']; + $component->name = $override['name']; } // We can do this for the description too. if (isset($override['description'])) { - $server->description = $override['description']; + $component->description = $override['description']; + } + // The index object has a server property. + if (isset($override['server'])) { + $component->server = $override['server']; + } + // The index object has a read_only property. + if (isset($override['read_only'])) { + $component->read_only = $override['read_only']; } + // Override each option, one by one, so we do not have to set the full // array in the override. if (!empty($override['options']) && is_array($override['options'])) { foreach ($override['options'] as $key => $value) { - $server->options[$key] = $value; + $component->options[$key] = $value; } } } @@ -133,6 +191,7 @@ function _search_api_override_apply_single_override($server, $override) { function search_api_override_settings() { $mode = variable_get('search_api_override_mode', ''); $servers = variable_get('search_api_override_servers', array()); + $indexes = variable_get('search_api_override_indexes', array()); // If none of the current settings is provided, we fall back to one of the // old formats. @@ -143,6 +202,7 @@ function search_api_override_settings() { return array( 'mode' => $mode, 'servers' => $servers, + 'indexes' => $indexes, ); } @@ -232,10 +292,58 @@ function search_api_override_form_search_api_admin_server_edit_alter(&$form, &$f } /** + * Implements hook_form_search_api_admin_index_edit_alter(). + */ +function search_api_override_form_search_api_admin_index_edit_alter(&$form, &$form_state) { + // We provide information about the index settings beeing overriden, if + // configuration for this index exists. + $index = $form_state['index']; + $settings = search_api_override_settings(); + if (!empty($settings['mode']) && !empty($settings['indexes'][$index->machine_name])) { + switch ($settings['mode']) { + case 'default': + // When features is enabled we provide additional information on where + // the settings are stored. + if (module_exists('features')) { + $components = features_get_component_map('search_api_index'); + if (!empty($components[$index->machine_name])) { + $message = t('Settings for this index are provided in the Search API Override configuration. The next time an associated feature is reverted, the settings will be reset to that configuration.'); + $message .= '
' . t('List of associated features: @list.', array('@list' => implode(', ', $components[$index->machine_name]))); + } + else { + $message = t('Settings for this index are provided in the Search API Override configuration. But there is no feature holding these settings, so you cannot revert them.'); + } + } + else { + $message = t('Settings for this index are provided in the Search API Override configuration. But Features module is not activated, so you could control reverting these settings.'); + } + + break; + case 'load'; + $message = t('The settings for this index are provided by Search API Override in "load" mode. You cannot change the settings for that configuration.'); + + // @todo: maybe disable appropriate form fields. + break; + } + + // Output the message for any of the two modes. + if (!empty($message)) { + // Add the current configuration to the message. + $config = format_string('$conf[\'search_api_override_indexes\'][\'@name\'] = @vardump;', array( + '@name' => $index->machine_name, + '@vardump' => var_export($settings['indexes'][$index->machine_name], TRUE), + )); + $message .= '
' . t('The overriding configuration is:
!config;
You may change that in your settings.php.', array('!config' => $config)); + drupal_set_message($message, 'warning'); + } + } +} + +/** * Implements hook_form_features_admin_components_alter(). */ function search_api_override_form_features_admin_components_alter(&$form, &$form_state) { - // We add a warning to the features overview page, when the settings of a + // We add a warning to the overview page of a feature, when the settings of a // Search API server are provided by Search API Override. // We need the module name to identify the feature, otherwise we cannot @@ -247,30 +355,33 @@ function search_api_override_form_features_admin_components_alter(&$form, &$form $feature_name = $form['module']['#value']; $feature_info = features_get_features($feature_name); - // If the feature holds search_api_server configuration, we check them against - // our settings. - if (!empty($feature_info->info['features']['search_api_server'])) { - - $feature_servers = $feature_info->info['features']['search_api_server']; + $components = array('server', 'index'); + foreach($components as $component) { + // If the feature holds search_api_server configuration, we check them against + // our settings. + $component_plural = ($component == 'server') ? 'servers' : 'indexes'; + if (!empty($feature_info->info['features']['search_api_' . $component])) { + $feature_items = $feature_info->info['features']['search_api_' . $component]; + $settings = search_api_override_settings(); + + if (!empty($settings[$component_plural])) { + $intersect = array_intersect($feature_items, array_keys($settings[$component_plural])); + if (count($intersect)) { + // We provide a warning message with links to the search api server|index + // settings for the overriden servers|indexes. + $links = array(); + foreach ($intersect as $item) { - $settings = search_api_override_settings(); - - if (!empty($settings['servers'])) { - $intersect = array_intersect($feature_servers, array_keys($settings['servers'])); - if (count($intersect)) { - // We provide a warning message with links to the search api server - // settings for the overriden servers. - $links = array(); - foreach ($intersect as $server) { - $links[] = l($server, 'admin/config/search/search_api/server/' . $server . '/edit', array( - 'attributes' => array('title' => t('Search API server @server settings page', array('@server' => $server))) + $links[] = l($item, 'admin/config/search/search_api/' . $component . '/' . $item . '/edit', array( + 'attributes' => array('title' => t('Search API server @item settings page', array('@item' => $item))) + )); + } + $message = t('Settings for some Search API servers|indexes are overriden via %mode-mode. Look at their settings page for more details: !links.', array( + '!links' => implode(', ', $links), + '%mode' => $settings['mode'], )); + drupal_set_message($message, 'warning'); } - $message = t('Server settings for some Search API servers are overriden via %mode-mode. Look at their settings page for more details: !links.', array( - '!links' => implode(', ', $links), - '%mode' => $settings['mode'], - )); - drupal_set_message($message, 'warning'); } } }