Issue #1831802 by znerol: Fixed Allow implementors of hook_search_admin() to add #submit on the form.

8.0.x
catch 2013-01-17 10:01:22 +00:00
parent 27743bd8ca
commit a20c323eb3
5 changed files with 89 additions and 3 deletions

View File

@ -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.
*/

View File

@ -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);
}
}

View File

@ -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' => '<em>' . 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.') . '</em>'
);
// 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;
}

View File

@ -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();
}