diff --git a/core/modules/search/lib/Drupal/search/Tests/SearchConfigSettingsFormTest.php b/core/modules/search/lib/Drupal/search/Tests/SearchConfigSettingsFormTest.php index 066da60..1641a99 100644 --- a/core/modules/search/lib/Drupal/search/Tests/SearchConfigSettingsFormTest.php +++ b/core/modules/search/lib/Drupal/search/Tests/SearchConfigSettingsFormTest.php @@ -86,6 +86,49 @@ function testSearchSettingsPage() { } /** + * Verify module-supplied settings form. + */ + function testSearchModuleSettingsPage() { + + // Test that the settings form displays the correct count of items left to index. + $this->drupalGet('admin/config/search/settings'); + + // Ensure that the settings fieldset for the test module is not present on + // the page + $this->assertNoText(t('Extra type settings')); + $this->assertNoText(t('Boost method')); + + // Ensure that the test module is listed as an option + $this->assertTrue($this->xpath('//input[@id="edit-active-modules-search-extra-type"]'), 'Checkbox for activating search for an extra module is visible'); + $this->assertTrue($this->xpath('//input[@id="edit-default-module-search-extra-type"]'), 'Radio button for setting extra module as default search module is visible'); + + // Enable search for the test module + $edit['active_modules[search_extra_type]'] = 'search_extra_type'; + $edit['default_module'] = 'search_extra_type'; + $this->drupalPost('admin/config/search/settings', $edit, t('Save configuration')); + + // Ensure that the settings fieldset is visible after enabling search for + // the test module + $this->assertText(t('Extra type settings')); + $this->assertText(t('Boost method')); + + // Ensure that the default setting was picked up from the default config + $this->assertTrue($this->xpath('//select[@id="edit-extra-type-settings-boost"]//option[@value="bi" and @selected="selected"]'), 'Module specific settings are picked up from the default config'); + + // Change extra type setting and also modify a common search setting. + $edit = array( + 'extra_type_settings[boost]' => 'ii', + 'minimum_word_size' => 5, + ); + $this->drupalPost('admin/config/search/settings', $edit, t('Save configuration')); + + // Ensure that the modifications took effect. + $this->assertText(t('The configuration options have been saved.')); + $this->assertTrue($this->xpath('//select[@id="edit-extra-type-settings-boost"]//option[@value="ii" and @selected="selected"]'), 'Module specific settings can be changed'); + $this->assertTrue($this->xpath('//input[@id="edit-minimum-word-size" and @value="5"]'), 'Common search settings can be modified if a module-specific form is active'); + } + + /** * Verify that you can disable individual search modules. */ function testSearchModuleDisabling() { diff --git a/core/modules/search/search.admin.inc b/core/modules/search/search.admin.inc index 57e5d6b..628a921 100644 --- a/core/modules/search/search.admin.inc +++ b/core/modules/search/search.admin.inc @@ -5,6 +5,8 @@ * Admin page callbacks for the search module. */ +use Drupal\Component\Utility\NestedArray; + /** * Menu callback: confirm wiping of the index. */ @@ -129,7 +131,7 @@ function search_admin_settings($form, &$form_state) { foreach ($config->get('active_modules') as $module) { $added_form = module_invoke($module, 'search_admin'); if (is_array($added_form)) { - $form = array_merge($form, $added_form); + $form = NestedArray::mergeDeep($form, $added_form); } } diff --git a/core/modules/search/search.api.php b/core/modules/search/search.api.php index be6dd4d..7d625c8 100644 --- a/core/modules/search/search.api.php +++ b/core/modules/search/search.api.php @@ -137,20 +137,25 @@ function hook_search_admin() { '#title' => t('Content ranking'), ); $form['content_ranking']['#theme'] = 'node_search_admin'; + $form['content_ranking']['#tree'] = TRUE; $form['content_ranking']['info'] = array( '#value' => '' . t('The following numbers control which properties the content search should favor when ordering the results. Higher numbers mean more influence, zero means the property is ignored. Changing these numbers does not require the search index to be rebuilt. Changes take effect immediately.') . '' ); // Note: reversed to reflect that higher number = higher ranking. $options = drupal_map_assoc(range(0, 10)); + $ranks = config('node.settings')->get('search_rank'); foreach (module_invoke_all('ranking') as $var => $values) { - $form['content_ranking']['factors']['node_rank_' . $var] = array( + $form['content_ranking']['factors'][$var] = array( '#title' => $values['title'], '#type' => 'select', '#options' => $options, - '#default_value' => variable_get('node_rank_' . $var, 0), + '#default_value' => isset($ranks[$var]) ? $ranks[$var] : 0, ); } + + $form['#submit'][] = 'node_search_admin_submit'; + return $form; } diff --git a/core/modules/search/tests/modules/search_extra_type/config/search_extra_type.settings.yml b/core/modules/search/tests/modules/search_extra_type/config/search_extra_type.settings.yml new file mode 100644 index 0000000..db64246 --- /dev/null +++ b/core/modules/search/tests/modules/search_extra_type/config/search_extra_type.settings.yml @@ -0,0 +1 @@ +boost: bi diff --git a/core/modules/search/tests/modules/search_extra_type/search_extra_type.module b/core/modules/search/tests/modules/search_extra_type/search_extra_type.module index 80c050c..a983b21 100644 --- a/core/modules/search/tests/modules/search_extra_type/search_extra_type.module +++ b/core/modules/search/tests/modules/search_extra_type/search_extra_type.module @@ -67,3 +67,38 @@ function search_extra_type_search_page($results) { return $output; } + +/** + * Implements hook_search_admin(). + */ +function search_extra_type_search_admin() { + // Output form for defining rank factor weights. + $form['extra_type_settings'] = array( + '#type' => 'fieldset', + '#title' => t('Extra type settings'), + '#tree' => TRUE, + ); + + $form['extra_type_settings']['boost'] = array( + '#type' => 'select', + '#title' => t('Boost method'), + '#options' => array( + 'bi' => t('Bistromathic'), + 'ii' => t('Infinite Improbability'), + ), + '#default_value' => config('search_extra_type.settings')->get('boost'), + ); + + $form['#submit'][] = 'search_extra_type_admin_submit'; + + return $form; +} + +/** + * Form API callback: Save admin settings + */ +function search_extra_type_admin_submit($form, &$form_state) { + config('search_extra_type.settings') + ->set('boost', $form_state['values']['extra_type_settings']['boost']) + ->save(); +}