From a20c323eb360d04f3678d2b5b70791c64e97dc58 Mon Sep 17 00:00:00 2001 From: catch Date: Thu, 17 Jan 2013 10:01:22 +0000 Subject: [PATCH] Issue #1831802 by znerol: Fixed Allow implementors of hook_search_admin() to add #submit on the form. --- .../Tests/SearchConfigSettingsFormTest.php | 43 +++++++++++++++++++ core/modules/search/search.admin.inc | 4 +- core/modules/search/search.api.php | 9 +++- .../config/search_extra_type.settings.yml | 1 + .../search_extra_type.module | 35 +++++++++++++++ 5 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 core/modules/search/tests/modules/search_extra_type/config/search_extra_type.settings.yml diff --git a/core/modules/search/lib/Drupal/search/Tests/SearchConfigSettingsFormTest.php b/core/modules/search/lib/Drupal/search/Tests/SearchConfigSettingsFormTest.php index ab525dd479f..3e80c725195 100644 --- a/core/modules/search/lib/Drupal/search/Tests/SearchConfigSettingsFormTest.php +++ b/core/modules/search/lib/Drupal/search/Tests/SearchConfigSettingsFormTest.php @@ -83,6 +83,49 @@ class SearchConfigSettingsFormTest extends SearchTestBase { $this->assertNoText(t('The configuration options have been saved.'), 'Form does not save with an invalid word length.'); } + /** + * 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. */ diff --git a/core/modules/search/search.admin.inc b/core/modules/search/search.admin.inc index ef70eb76d64..6997946f1d8 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 6429f7a8532..1e431a512b1 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 00000000000..db6424665a9 --- /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 80c050c21f7..a983b21e938 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(); +}