Drupal 7 查看上下文过滤器或逻辑

Posted

技术标签:

【中文标题】Drupal 7 查看上下文过滤器或逻辑【英文标题】:Drupal 7 Views contextual filters OR logic 【发布时间】:2012-11-23 15:21:03 【问题描述】:

上下文过滤器应用于 AND 逻辑。有没有办法在 OR 逻辑中有两个或多个上下文过滤器?

【问题讨论】:

【参考方案1】:

在视图 7.x-3.5 中,这可能无法使用 UI。

假设您的模块名称是my_module

将以下内容添加到您的 my_module.module 文件中

<?php
function my_moudle_views_api() 
  return array(
    'api' => 3,
  );

?>

以下到 my_module.views.inc

<?php
function eb_mine_views_query_alter(&$view, &$query) 
  if ($view->name == 'statuser') 
    dsm($query, 'before');
    $query->where[0]['type'] = 'OR';
    dsm($query, 'after');
  

?>

来源:http://drupal.org/node/1451218#comment-6136692

而在早期版本的视图中,上下文过滤器的 AND/OR 选项将由视图中第一组静态过滤器的选项决定。

Grouping of contextual filters
Even though contextual filters do not appear in the "and/or" user interface for sorting and grouping regular filters, contextual filters are always added to the first group of filters. Thus the order of the groups can cause the contextual filter to have entirely different effects on the results of a view that has contextual filters. Even though differences might not be apparent through the user interface.

Multiple contextual filters are therefore always in the same "and/or" group of filters, and can not be placed in different groups. There is an effort to add this feature.

【讨论】:

我必须重新启用我的模块才能包含 inc 文件。我也试过 hook_views_pre_execute 没有成功 这对我有用!不过,我不得不做一个额外的步骤,我对这个相关问题发表了新的评论。我希望 Views 人员尽快为上下文过滤器添加 OR 分组。 你可能想指出这是从哪里来的...drupal.org/comment/6136692#comment-6136692 你有一个错字,兄弟。这是 my_module.views.inc 不是 my_module.view.inc【参考方案2】:

这是一个允许“将参数传递到普通过滤器 [UI]”的补丁 https://drupal.org/node/357082

【讨论】:

【参考方案3】:

如果您坚持只有一个 UI 选项,那么这个模块就可以解决问题。我已经对其进行了测试,尽管似乎确实存在问题的可能性,但它会定期维护。当我将它安装在我的自定义 Drupal 发行版上时,它似乎没有破坏任何东西(尽管我确实修改了一些代码以消除我的自定义代码出现一些问题的可能性)。

https://www.drupal.org/project/views_contextual_filters_or

此外,如果您希望进一步隔离此模块的行为(使其仅适用于特定视图或适用于特定内容类型的视图),代码本身很容易导出到您自己的模块。

【讨论】:

以上是关于Drupal 7 查看上下文过滤器或逻辑的主要内容,如果未能解决你的问题,请参考以下文章

Drupal 7 视图上下文过滤器

Drupal 7 和视图,通过多个/组合字段搜索的上下文过滤器。是不是可以?

Drupal 7 视图 - 如何将上下文过滤器与常规过滤器(使用 OR)结合起来?

具有默认值上下文过滤器的 Drupal 7 视图块不会在页面上过滤(Mongo)

如何在 Drupal 7 中的 URL 中获取视图(上下文过滤器)中的结果

如果未定义第二个过滤器,则具有 2 个上下文过滤器的 Drupal 7 视图不起作用