diff --git a/core/modules/search/lib/Drupal/search/Form/SearchSettingsForm.php b/core/modules/search/lib/Drupal/search/Form/SearchSettingsForm.php new file mode 100644 index 0000000..ca89535 --- /dev/null +++ b/core/modules/search/lib/Drupal/search/Form/SearchSettingsForm.php @@ -0,0 +1,210 @@ +configFactory = $config_factory; + $this->moduleHandler = $module_handler; + } + + /** + * Implements \Drupal\Core\ControllerInterface::create(). + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('config.factory'), + $container->get('module_handler') + ); + } + + /** + * Implements \Drupal\Core\Form\FormInterface::getFormID(). + */ + public function getFormID() { + return 'search_admin_settings'; + } + + /** + * Helper function to get real module names. + */ + private function SearchGetModuleNames() { + $search_info = search_get_info(TRUE); + $names = system_get_module_info('name'); + $names = array_intersect_key($names, $search_info); + asort($names, SORT_STRING); + return $names; + } + + /** + * Implements \Drupal\Core\Form\FormInterface::buildForm(). + */ + public function buildForm(array $form, array &$form_state) { + $config = $this->configFactory->get('search.settings'); + // Collect some stats + $remaining = 0; + $total = 0; + foreach ($config->get('active_modules') as $module) { + if ($status = $this->moduleHandler->invoke($module, 'search_status')) { + $remaining += $status['remaining']; + $total += $status['total']; + } + } + + module_load_include('inc', 'search', 'search.admin'); + $count = format_plural($remaining, 'There is 1 item left to index.', 'There are @count items left to index.'); + $percentage = ((int)min(100, 100 * ($total - $remaining) / max(1, $total))) . '%'; + $status = '

' . t('%percentage of the site has been indexed.', array('%percentage' => $percentage)) . ' ' . $count . '

'; + $form['status'] = array('#type' => 'details', '#title' => t('Indexing status')); + $form['status']['status'] = array('#markup' => $status); + $form['status']['wipe'] = array('#type' => 'submit', '#value' => t('Re-index site'), '#submit' => array('search_admin_reindex_submit')); + + $items = drupal_map_assoc(array(10, 20, 50, 100, 200, 500)); + + // Indexing throttle: + $form['indexing_throttle'] = array( + '#type' => 'details', + '#title' => t('Indexing throttle') + ); + $form['indexing_throttle']['cron_limit'] = array( + '#type' => 'select', + '#title' => t('Number of items to index per cron run'), + '#default_value' => $config->get('index.cron_limit'), + '#options' => $items, + '#description' => t('The maximum number of items indexed in each pass of a cron maintenance task. If necessary, reduce the number of items to prevent timeouts and memory errors while indexing.', array('@cron' => url('admin/reports/status'))) + ); + // Indexing settings: + $form['indexing_settings'] = array( + '#type' => 'details', + '#title' => t('Indexing settings') + ); + $form['indexing_settings']['info'] = array( + '#markup' => t('

Changing the settings below will cause the site index to be rebuilt. The search index is not cleared but systematically updated to reflect the new settings. Searching will continue to work but new content won\'t be indexed until all existing content has been re-indexed.

The default settings should be appropriate for the majority of sites.

') + ); + $form['indexing_settings']['minimum_word_size'] = array( + '#type' => 'number', + '#title' => t('Minimum word length to index'), + '#default_value' => $config->get('index.minimum_word_size'), + '#min' => 1, + '#max' => 1000, + '#description' => t('The number of characters a word has to be to be indexed. A lower setting means better search result ranking, but also a larger database. Each search query must contain at least one keyword that is this size (or longer).') + ); + $form['indexing_settings']['overlap_cjk'] = array( + '#type' => 'checkbox', + '#title' => t('Simple CJK handling'), + '#default_value' => $config->get('index.overlap_cjk'), + '#description' => t('Whether to apply a simple Chinese/Japanese/Korean tokenizer based on overlapping sequences. Turn this off if you want to use an external preprocessor for this instead. Does not affect other languages.') + ); + + $form['active'] = array( + '#type' => 'details', + '#title' => t('Active search modules') + ); + $module_options = $this->SearchGetModuleNames(); + $form['active']['active_modules'] = array( + '#type' => 'checkboxes', + '#title' => t('Active modules'), + '#title_display' => 'invisible', + '#default_value' => $config->get('active_modules'), + '#options' => $module_options, + '#description' => t('Choose which search modules are active from the available modules.') + ); + $form['active']['default_module'] = array( + '#title' => t('Default search module'), + '#type' => 'radios', + '#default_value' => $config->get('default_module'), + '#options' => $module_options, + '#description' => t('Choose which search module is the default.') + ); + + // Per module settings + foreach ($config->get('active_modules') as $module) { + $added_form = $this->moduleHandler->invoke($module, 'search_admin'); + if (is_array($added_form)) { + $form = NestedArray::mergeDeep($form, $added_form); + } + } + + $form['#submit'][] = array($form_state['build_info']['callback_object'], 'submitForm'); + + return parent::buildForm($form, $form_state); + } + + /** + * Implements \Drupal\Core\Form\FormInterface::validateForm(). + */ + public function validateForm(array &$form, array &$form_state) { + parent::validateForm($form, $form_state); + + // Check whether we selected a valid default. + if ($form_state['triggering_element']['#value'] != t('Reset to defaults')) { + $new_modules = array_filter($form_state['values']['active_modules']); + $default = $form_state['values']['default_module']; + if (!in_array($default, $new_modules, TRUE)) { + form_set_error('default_module', t('Your default search module is not selected as an active module.')); + } + } + } + + /** + * Implements \Drupal\Core\Form\FormInterface::submitForm(). + */ + public function submitForm(array &$form, array &$form_state) { + parent::submitForm($form, $form_state); + + $config = $this->configFactory->get('search.settings'); + // If these settings change, the index needs to be rebuilt. + if (($config->get('index.minimum_word_size') != $form_state['values']['minimum_word_size']) || ($config->get('index.overlap_cjk') != $form_state['values']['overlap_cjk'])) { + $config->set('index.minimum_word_size', $form_state['values']['minimum_word_size']); + $config->set('index.overlap_cjk', $form_state['values']['overlap_cjk']); + drupal_set_message(t('The index will be rebuilt.')); + search_reindex(); + } + $config->set('index.cron_limit', $form_state['values']['cron_limit']); + $config->set('default_module', $form_state['values']['default_module']); + + // Check whether we are resetting the values. + if ($form_state['triggering_element']['#value'] == t('Reset to defaults')) { + $new_modules = array('node', 'user'); + } + else { + $new_modules = array_filter($form_state['values']['active_modules']); + } + if ($config->get('active_modules') != $new_modules) { + $config->set('active_modules', $new_modules); + drupal_set_message(t('The active search modules have been changed.')); + state()->set('menu_rebuild_needed', TRUE); + } + $config->save(); + } +} diff --git a/core/modules/search/search.admin.inc b/core/modules/search/search.admin.inc index a63cf47..e76c6e4 100644 --- a/core/modules/search/search.admin.inc +++ b/core/modules/search/search.admin.inc @@ -30,160 +30,6 @@ function search_reindex_confirm_submit(&$form, &$form_state) { } } -/** - * Helper function to get real module names. - */ -function _search_get_module_names() { - $search_info = search_get_info(TRUE); - $names = system_get_module_info('name'); - $names = array_intersect_key($names, $search_info); - asort($names, SORT_STRING); - return $names; -} - -/** - * Form constructor for the Search module's settings page. - * - * @see search_admin_settings_validate() - * @see search_admin_settings_submit() - * @see search_admin_reindex_submit() - * - * @ingroup forms - */ -function search_admin_settings($form, &$form_state) { - $config = config('search.settings'); - - // Collect some stats - $remaining = 0; - $total = 0; - foreach ($config->get('active_modules') as $module) { - if ($status = module_invoke($module, 'search_status')) { - $remaining += $status['remaining']; - $total += $status['total']; - } - } - - $count = format_plural($remaining, 'There is 1 item left to index.', 'There are @count items left to index.'); - $percentage = ((int)min(100, 100 * ($total - $remaining) / max(1, $total))) . '%'; - $status = '

' . t('%percentage of the site has been indexed.', array('%percentage' => $percentage)) . ' ' . $count . '

'; - $form['status'] = array('#type' => 'details', '#title' => t('Indexing status')); - $form['status']['status'] = array('#markup' => $status); - $form['status']['wipe'] = array('#type' => 'submit', '#value' => t('Re-index site'), '#submit' => array('search_admin_reindex_submit')); - - $items = drupal_map_assoc(array(10, 20, 50, 100, 200, 500)); - - // Indexing throttle: - $form['indexing_throttle'] = array( - '#type' => 'details', - '#title' => t('Indexing throttle') - ); - $form['indexing_throttle']['cron_limit'] = array( - '#type' => 'select', - '#title' => t('Number of items to index per cron run'), - '#default_value' => $config->get('index.cron_limit'), - '#options' => $items, - '#description' => t('The maximum number of items indexed in each pass of a cron maintenance task. If necessary, reduce the number of items to prevent timeouts and memory errors while indexing.', array('@cron' => url('admin/reports/status'))) - ); - // Indexing settings: - $form['indexing_settings'] = array( - '#type' => 'details', - '#title' => t('Indexing settings') - ); - $form['indexing_settings']['info'] = array( - '#markup' => t('

Changing the settings below will cause the site index to be rebuilt. The search index is not cleared but systematically updated to reflect the new settings. Searching will continue to work but new content won\'t be indexed until all existing content has been re-indexed.

The default settings should be appropriate for the majority of sites.

') - ); - $form['indexing_settings']['minimum_word_size'] = array( - '#type' => 'number', - '#title' => t('Minimum word length to index'), - '#default_value' => $config->get('index.minimum_word_size'), - '#min' => 1, - '#max' => 1000, - '#description' => t('The number of characters a word has to be to be indexed. A lower setting means better search result ranking, but also a larger database. Each search query must contain at least one keyword that is this size (or longer).') - ); - $form['indexing_settings']['overlap_cjk'] = array( - '#type' => 'checkbox', - '#title' => t('Simple CJK handling'), - '#default_value' => $config->get('index.overlap_cjk'), - '#description' => t('Whether to apply a simple Chinese/Japanese/Korean tokenizer based on overlapping sequences. Turn this off if you want to use an external preprocessor for this instead. Does not affect other languages.') - ); - - $form['active'] = array( - '#type' => 'details', - '#title' => t('Active search modules') - ); - $module_options = _search_get_module_names(); - $form['active']['active_modules'] = array( - '#type' => 'checkboxes', - '#title' => t('Active modules'), - '#title_display' => 'invisible', - '#default_value' => $config->get('active_modules'), - '#options' => $module_options, - '#description' => t('Choose which search modules are active from the available modules.') - ); - $form['active']['default_module'] = array( - '#title' => t('Default search module'), - '#type' => 'radios', - '#default_value' => $config->get('default_module'), - '#options' => $module_options, - '#description' => t('Choose which search module is the default.') - ); - $form['#validate'][] = 'search_admin_settings_validate'; - $form['#submit'][] = 'search_admin_settings_submit'; - - // Per module settings - foreach ($config->get('active_modules') as $module) { - $added_form = module_invoke($module, 'search_admin'); - if (is_array($added_form)) { - $form = NestedArray::mergeDeep($form, $added_form); - } - } - - return system_config_form($form, $form_state); -} - -/** - * Form validation handler for search_admin_settings(). - */ -function search_admin_settings_validate($form, &$form_state) { - // Check whether we selected a valid default. - if ($form_state['triggering_element']['#value'] != t('Reset to defaults')) { - $new_modules = array_filter($form_state['values']['active_modules']); - $default = $form_state['values']['default_module']; - if (!in_array($default, $new_modules, TRUE)) { - form_set_error('default_module', t('Your default search module is not selected as an active module.')); - } - } -} - -/** - * Form submission handler for search_admin_settings(). - */ -function search_admin_settings_submit($form, &$form_state) { - $config = config('search.settings'); - // If these settings change, the index needs to be rebuilt. - if (($config->get('index.minimum_word_size') != $form_state['values']['minimum_word_size']) || ($config->get('index.overlap_cjk') != $form_state['values']['overlap_cjk'])) { - $config->set('index.minimum_word_size', $form_state['values']['minimum_word_size']); - $config->set('index.overlap_cjk', $form_state['values']['overlap_cjk']); - drupal_set_message(t('The index will be rebuilt.')); - search_reindex(); - } - $config->set('index.cron_limit', $form_state['values']['cron_limit']); - $config->set('default_module', $form_state['values']['default_module']); - - // Check whether we are resetting the values. - if ($form_state['triggering_element']['#value'] == t('Reset to defaults')) { - $new_modules = array('node', 'user'); - } - else { - $new_modules = array_filter($form_state['values']['active_modules']); - } - if ($config->get('active_modules') != $new_modules) { - $config->set('active_modules', $new_modules); - drupal_set_message(t('The active search modules have been changed.')); - state()->set('menu_rebuild_needed', TRUE); - } - $config->save(); -} /** * Form submission handler for the reindex button on search_admin_settings(). diff --git a/core/modules/search/search.module b/core/modules/search/search.module index 375c43f..4345b98 100644 --- a/core/modules/search/search.module +++ b/core/modules/search/search.module @@ -157,8 +157,7 @@ function search_menu() { $items['admin/config/search/settings'] = array( 'title' => 'Search settings', 'description' => 'Configure relevance settings for search and other indexing options.', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('search_admin_settings'), + 'page callback' => 'NOT_USED', 'access arguments' => array('administer search'), 'weight' => -10, 'file' => 'search.admin.inc', diff --git a/core/modules/search/search.routing.yml b/core/modules/search/search.routing.yml new file mode 100644 index 0000000..f503672 --- /dev/null +++ b/core/modules/search/search.routing.yml @@ -0,0 +1,6 @@ +search_settings: + pattern: '/admin/config/search/settings' + defaults: + _form: 'Drupal\search\Form\SearchSettingsForm' + requirements: + _permission: 'administer search'