繁体   English   中英

Drupal View过滤器中的OR运算符

[英]OR operator in Drupal View Filters

我需要在Drupal View中的某些过滤器之间实现OR运算符。 默认情况下,Drupal AND的每个过滤器都在一起。

通过使用

hook_views_query_alter(&$view, &$query)

我可以访问查询(var $ query),我可以更改:

$query->where[0]['type'] 

'或',或

$query->group_operator 

'OR'

但问题是,我到处都不需要OR。 我已经尝试将它们分别更改为OR,并且它不会产生预期的结果。

它似乎改变了这些值,把OR放在任何地方,而我需要=>(滤波器1和滤波器2)或(滤波器3),所以只需1或。

我可以检查View的查询,复制它,修改它,然后通过db_query运行它,但那只是脏的..

有什么建议么 ?

Thx提前。

如果你正在使用Views 3 / Drupal 7并寻找这个问题的答案,它会直接进入Views。 在过滤器旁边显示“添加”的位置,单击下拉列表,然后单击“和/或;重新排列”。 从那里应该是显而易见的。

不幸的是,这仍然是Views2中缺少的功能。 它早已被要求并且很久以前得到了承诺,但似乎是一项棘手的工作, 现在计划用于Views3

在此期间,您可以尝试该线程中提到的Views Or模块。 截至今天,它仍然处于开发状态,但似乎是积极维护并且问题队列看起来并不坏,所以你可能想尝试一下。

如果你想用view_query_alter钩子做,你应该使用$ query-> add_where(),你可以指定它是AND还是OR。 来自views / include / query.inc

  /**
   * Add a simple WHERE clause to the query. The caller is responsible for
   * ensuring that all fields are fully qualified (TABLE.FIELD) and that
   * the table already exists in the query.
   *
   * @param $group
   *   The WHERE group to add these to; groups are used to create AND/OR
   *   sections. Groups cannot be nested. Use 0 as the default group.
   *   If the group does not yet exist it will be created as an AND group.
   * @param $clause
   *   The actual clause to add. When adding a where clause it is important
   *   that all tables are addressed by the alias provided by add_table or
   *   ensure_table and that all fields are addressed by their alias wehn
   *   possible. Please use %d and %s for arguments.
   * @param ...
   *   A number of arguments as used in db_query(). May be many args or one
   *   array full of args.
   */
  function add_where($group, $clause)

我通过连接字符串添加它。

它与实现相对具体 - 人们需要在下面的代码中使用字段来匹配OR - node.title,并在这种情况下将字段与 - node_revisions.body匹配。

额外的代码片段,以确保node_revisions.body在查询中。

/**
 * Implementation of hook_views_api().
 */
function eventsor_views_api() { // your module name into hook_views_api
  return array(
  'api' => 2,
  // might not need the line below, but in any case, the last arg is the name of your module
  'path' => drupal_get_path('module', 'eventsor'),
 );
}

/**
 *
 * @param string $form
 * @param type $form_state
 * @param type $form_id 
 */
function eventsor_views_query_alter(&$view, &$query) {

  switch ($view->name) {
    case 'Events':
      _eventsor_composite_filter($query);
      break;
  }
}

/**
 * Add to the where clause.
 * @param type $query 
 */
function _eventsor_composite_filter(&$query) {
  // If we see "UPPER(node.title) LIKE UPPER('%%%s%%')" - then add and to it.
  if (isset($query->where)) {

    $where_count = 0;
    foreach ($query->where as $where) {
      $clause_count = 0;
      if (isset($where['clauses'])) {
        foreach ($where['clauses'] as $clause) {
          $search_where_clause = "UPPER(node.title) LIKE UPPER('%%%s%%')";
          // node_data_field_long_description.field_long_description_value
          $desirable_where_clause = "UPPER(CONCAT_WS(' ', node.title, node_revisions.body)) LIKE UPPER('%%%s%%')";
          if ($clause == $search_where_clause) {
            //  $query->add_where('or', 'revisions.body = %s'); - outside of what we are looking for
            $query->where[$where_count]['clauses'][$clause_count] = $desirable_where_clause;

            // Add the field to the view, just in case.
            if (!isset($query->fields['node_revisions_body'])) {
              $query->fields['node_revisions_body'] = array(
                'field' => 'body',
                'table' => 'node_revisions',
                'alias' => 'node_revisions_body'
              );
            }
          }
          $clause_count++;
        }
      }
      $where_count++;
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM