【问题标题】:Drupal 8 - how to get exposed filters value in preprocess_views_viewDrupal 8 - 如何在 preprocess_views_view 中获取暴露的过滤器值
【发布时间】:2018-07-14 21:04:45
【问题描述】:

我正在使用 preprocess_views_view 定义一些新变量并将它们传递给 twig 模板。

为了定义这些变量,我需要访问公开的过滤器输入值,但我似乎无法弄清楚如何:

function my_modules_preprocess_views_view(&$variables) {
  $view = $variables['view'];
  // Here I would need to access the exposed filters value
  $exposed_filter_value = "the_value";
  $variables["foo"] = "Something based on the exposed filters value";
}

如果有任何线索,我将不胜感激 - 干杯!

【问题讨论】:

    标签: drupal-views drupal-8 drupal-exposed-filter


    【解决方案1】:

    在您的主题或模块中的 hook_preprocess_views_view() 实现中:

    $values = $view->getExposedInput();
    // for example $values["color"];
    

    或者,您可以直接从views-view.html.twig 模板访问值:

    // Assuming `color` is configured to be the Filter identifier in the 
    // view's Filter Criteria exposed filter.
    {{ view.getExposedInput.color }}
    

    【讨论】:

    • 请注意:这仅在我关闭 AJAX 时对我有效。
    【解决方案2】:

    我在使用 @Hubert 的解决方案时遇到了问题,并设法让它发挥作用:

    $variables["searchInputValue"] = $view->exposed_raw_input['query'];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 2020-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多