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..3ec0ba1 --- /dev/null +++ b/core/modules/search/lib/Drupal/search/Form/SearchSettingsForm.php @@ -0,0 +1,231 @@ +configFactory = $config_factory; + $this->moduleHandler = $module_handler; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('config.factory'), + $container->get('module_handler') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormID() { + return 'search_admin_settings'; + } + + /** + * Returns names of available search modules. + * + * @return array + * An array of the names of enabled modules that call hook_search_info + * sorted into alphabetical order. + */ + protected 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; + } + + /** + * {@inheritdoc} + */ + 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']; + } + } + + $this->moduleHandler->loadAllIncludes('admin.inc'); + $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(array($form_state['build_info']['callback_object'], 'searchAdminReindexSubmit'))); + $form['#submit'][] = array($form_state['build_info']['callback_object'], 'submitForm'); + + $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); + } + + /** + * {@inheritdoc} + */ + 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.')); + } + } + } + + /** + * {@inheritdoc} + */ + 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(); + } + + /** + * Form submission handler for the reindex button on the search admin settings + * form. + */ + public function searchAdminReindexSubmit(array $form, array &$form_state) { + // send the user to the confirmation page + $form_state['redirect'] = 'admin/config/search/settings/reindex'; + } +} diff --git a/core/modules/search/search.admin.inc b/core/modules/search/search.admin.inc deleted file mode 100644 index 6f0825f..0000000 --- a/core/modules/search/search.admin.inc +++ /dev/null @@ -1,171 +0,0 @@ -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(). - */ -function search_admin_reindex_submit($form, &$form_state) { - // send the user to the confirmation page - $form_state['redirect'] = 'admin/config/search/settings/reindex'; -} diff --git a/core/modules/search/search.module b/core/modules/search/search.module index efaf031..9fc5cde 100644 --- a/core/modules/search/search.module +++ b/core/modules/search/search.module @@ -158,11 +158,8 @@ 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'), - 'access arguments' => array('administer search'), + 'route_name' => 'search_settings', 'weight' => -10, - 'file' => 'search.admin.inc', ); $items['admin/config/search/settings/reindex'] = array( 'title' => 'Clear index', diff --git a/core/modules/search/search.routing.yml b/core/modules/search/search.routing.yml index 5521e18..8acf650 100644 --- a/core/modules/search/search.routing.yml +++ b/core/modules/search/search.routing.yml @@ -1,3 +1,9 @@ +search_settings: + pattern: '/admin/config/search/settings' + defaults: + _form: 'Drupal\search\Form\SearchSettingsForm' + requirements: + _permission: 'administer search' search_reindex_confirm: pattern: '/admin/config/search/settings/reindex' defaults: