默认情况下,搜索页面仅显示10个结果。如何更改显示无限数量的结果?
默认情况下,搜索页面仅显示10个结果。如何更改显示无限数量的结果?
Answers:
我建议为此使用视图。视图附带了一个搜索视图(默认情况下处于禁用状态)。
通常将10个项目的限制硬编码到hook_search_execute()
实现中(例如node_search_execute()
):理论上,您可以实现查询hook_search_execute()
并将->limit(10)
查询更改为10
。
但是正如beeradb所述,仅使用Views覆盖核心搜索结果可能会更容易。
Drupal当前无法为您提供更改每页搜索结果数量的选项。正如@beeradb建议的那样,您可以使用“视图”模块(D7的视频教程,网址为http://lin-clark.com/blog/intro-drupal-7-intro-views-pt-1)。
对此有功能要求,请进入Drupal 8 http://drupal.org/node/702940。
在Drupal 7中,我能够使用自定义模块中的hook_query_alter来修改每页的核心搜索结果项。如果您熟悉在代码中使用钩子,则可以使用以下代码片段:
<?php
/**
* Implements hook_query_alter
*/
function MY_MODULE_query_alter(&$query) {
// This is a search result query and we want to change the
// number of search results per page to 16.
if ($query instanceof PagerDefault
&& ($tables = $query->getTables())
&& isset($tables['i']['table'])
&& $tables['i']['table'] === 'search_index') {
$query->limit(16);
}
}
我在视图模块中找不到“搜索视图”。我正在使用7.x-3.7视图。
为了在Drupal 7的一页上获得10多个搜索结果,我使用了search_config模块。我使用了最新版本“ 7.x-1.1-beta2”。
启用该模块后,您可以在“附加节点搜索配置”部分下的“配置”->“搜索”和“元数据”->“搜索设置(admin / config / search / settings)”中配置寻呼机限制。您将在此处看到“结果”部分,并且可以指定页面限制以在一页上显示10个以上的搜索结果。
默认的搜索视图似乎已从“视图”模块中删除,因此我迅速将其删除,您可以将其用作满足自己需求的起点。使用“视图”导入对话框并粘贴代码以将其导入。
$view = new view();
$view->name = 'search_test';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'search test';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'search test';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'input_required';
$handler->display->display_options['exposed_form']['options']['text_input_required'] = 'Enter some search keywords.';
$handler->display->display_options['exposed_form']['options']['text_input_required_format'] = 'filtered_html';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '20';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'fields';
/* No results behavior: Global: Text area */
$handler->display->display_options['empty']['area']['id'] = 'area';
$handler->display->display_options['empty']['area']['table'] = 'views';
$handler->display->display_options['empty']['area']['field'] = 'area';
$handler->display->display_options['empty']['area']['empty'] = TRUE;
$handler->display->display_options['empty']['area']['content'] = 'No results found.';
$handler->display->display_options['empty']['area']['format'] = 'filtered_html';
/* Field: Content: Title */
$handler->display->display_options['fields']['title']['id'] = 'title';
$handler->display->display_options['fields']['title']['table'] = 'node';
$handler->display->display_options['fields']['title']['field'] = 'title';
$handler->display->display_options['fields']['title']['label'] = '';
$handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE;
/* Filter criterion: Content: Published */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'node';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = 1;
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Search: Search Terms */
$handler->display->display_options['filters']['keys']['id'] = 'keys';
$handler->display->display_options['filters']['keys']['table'] = 'search_index';
$handler->display->display_options['filters']['keys']['field'] = 'keys';
$handler->display->display_options['filters']['keys']['operator'] = 'required';
$handler->display->display_options['filters']['keys']['exposed'] = TRUE;
$handler->display->display_options['filters']['keys']['expose']['operator_id'] = 'keys_op';
$handler->display->display_options['filters']['keys']['expose']['label'] = 'Search Terms';
$handler->display->display_options['filters']['keys']['expose']['operator'] = 'keys_op';
$handler->display->display_options['filters']['keys']['expose']['identifier'] = 'keys';
/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'search';