我试图从搜索页面URL中获得一个参数,这样我就可以将这个参数传递给一个链接。例如,我有一个搜索结果页面:http://domain.com/site-search?search_api_fulltext=foo的URL,在该页面上,我希望有一个指向:http://domain.com/other-search?search_api_fulltext=foo的链接
我尝试过{{ url('') }},但它只返回域和路径,而不返回参数。我一直在尝试像{{ app.request.requestUri }}这样的东西,但没有任何回报。
FWIW,我试图将这些链接包含在一个显示在搜索结果页面上的自定义块中。
发布于 2019-12-08 22:38:43
好吧,如果你在做定制块,那么你很可能是在做这样的事情.
$build = [];
$build['#theme'] = 'your_template',
return $build;然后在您的hook_theme()中。
hook_theme() {
return [
'your_template' => [
'render element' => 'children',
'template' = 'your-templante-name',
],
];
}您根本不需要预先处理该块,只需确保该块没有被缓存,请将此添加到您的块插件中。
请参阅如何防止块被缓存?
public function getCacheMaxAge() {
return 0;
}然后在块函数中:
$search_query = \Drupal::request()->get('search_api_fulltext');
$build = [];
$build['#theme'] = 'your_template',
$build['#search_query'] = $search_query,
return $build;
....
hook_theme() {
return [
'your_template' => [
'render element' => 'children',
'template' = 'your-templante-name',
'variables' => [
'search_query' => NULL,
],
],
];
}然后在你的小树枝模板里
{% if search_query %}
do what you want here {{search_query}}
{% endif%}https://drupal.stackexchange.com/questions/289059
复制相似问题