题
如何将默认的 Apache Solr URL 路径从“search/apachesolr_search/term”更改为其他路径?
解决方案
我通常这样做的方式是实现hook_menu_alter()。然后,您可以以确切的选择方式自定义URL:
/**
* Implements hook_menu_alter().
*/
function example_menu_alter(&$menu) {
// Ensure Apache Solr is the default and that the menu item exists.
if (variable_get('apachesolr_search_make_default', 0) && isset($menu['search/apachesolr/%menu_tail'])) {
$menu['search/%menu_tail'] = $menu['search/apachesolr/%menu_tail'];
unset($menu['search/apachesolr/%menu_tail']);
}
}
其他提示
如果您仅使用Apachesolr搜索模块,则不容易更改搜索路径。由于它取决于核心搜索模块,因此该路径几乎是硬编码的。它取决于搜索/{module}/%菜单菜单。如果你看 search_view(), ,搜索模块的回调,您会发现它调用 search_get_keys(), ,希望搜索键处于路径的特定部分。 Apachesolr搜索模块还使用此功能获取键,因此实现简单的hook_menu_alter()无法单独使用。
如这里的另一个答案中所述,如果您能够运行视图3.x,那么最好的选择是使用 Apachesolr视图模块. 。使用此模块,您可以轻松地为搜索结果定义任何数量的自定义路径。
如果您无法运行3.x,则需要使用form Alter(特别是search_form)和自定义菜单回调的组合来成功更改默认搜索路径。
如果将其放置在stetings.php中,则应该有效:
function custom_url_rewrite_outbound(&$path, &$options, $original_path) {
// Filter to get only the apache solr links with filters so it doesn't launch it for every link of our website
if ($path == 'search/apachesolr_search/' && strpos($options['query'], 'filters') !== FALSE) {
$new_path = $path.'?'.urldecode($options['query']);
// See if we have a url_alias for our new path
$sql = 'SELECT dst FROM {url_alias} WHERE src="%s"';
$row = db_result(db_query($sql, $new_path));
// If there is a dst url_alia, we change the path to it and erase the query
if ($row) {
$path = $row;
$options['query'] = '';
}
}
}
function custom_url_rewrite_inbound(&$result, $path, $path_language) {
// See if we have a url_alias for our new path
$sql = 'SELECT src FROM {url_alias} WHERE dst="%s"';
$row = db_result(db_query($sql, $path));
if ($row) {
// We found a source path
$parts = preg_split('/[\?\&]/', $row);
if (count($parts) > 1) {
$result = array_shift($parts);
// That's important because on my website, it doesn't work with the / at the end of result
if ($result[strlen($result) - 1] == '/') {
$result = substr($result, 0, strlen($result) - 1);
}
// Create the $_GET with the filter
foreach ($parts as $part) {
list($key, $value) = explode('=', $part);
$_GET[$key] = $value;
// Add this because the pager use the $_REQUEST variable to be set
$_REQUEST[$key] = $value;
}
}
}
}
然后,当您创建菜单条目时,将链接放到apache solr:search/apachesolr_search/?filters = tid:13
并为搜索创建一个URL别名/apachesolr_search/?filters = tid:13喜欢products/tv.html
你可以使用 solr视图 用于您的网站搜索。
- 创建 solr 视图。
- 添加页面显示你想要的路径
- 添加搜索文本作为过滤器。
- 块中暴露的形式
- 将块放在您希望搜索框所在的位置。
查看 使用hook_menu添加自定义搜索路径 由不断发展的网络的家伙。它谈到了他们如何编写自定义模块来为其SOLR搜索创建友好的URL。您可能需要对其进行一些调整,但这是一个很好的起点。
如何创建一个新的菜单回调,例如以下以使Apachesolr返回结果在该路径上呢?
$menu['search']['page callback'] = 'apachesolr_search_view';
或者,您可以修改执行此操作的贡献模块: Apache Solr自定义路径.
如果您只想将搜索/ apachesolr_search/更改为其他东西,例如“库存”,您可以尝试 全球重定向 模块。
诀窍是创建2个别名 - 一个用于搜索/ apachesolr_search/(用于刻面项目),另一个不带斜线(用于主搜索页面)。全局重定向可能拒绝为同一目的地创建2个别名,但是您可以将其直接插入DB。