简体   繁体   English

在Drupal视图中针对暴露的过滤器的自定义逻辑

[英]Custom logic for exposed filters in a Drupal view

I'm working on a Drupal site and would love some advice on this. 我正在Drupal网站上工作,并希望就此提供一些建议。 Currently, a user enters his level for a number of different skills. 当前,用户输入他的等级以获取多种不同的技能。 This is stored in a CCK integer field and exposed to the user as a drop-down widget containing the key/value pairs 1|Beginner, 2|Intermediate, 3|Advanced. 它存储在CCK整数字段中,并作为包含键/值对1 | Beginner,2 | Intermediate,3 | Advanced的下拉小部件显示给用户。

In a view, I expose the allowed values for each skill, which are presented to the user as checkboxes (using the Better Exposed Filters module) and then listed in a sortable table. 在视图中,我公开了每个技能的允许值,这些值作为复选框(使用“更好的暴露筛选器”模块显示给用户),然后在可排序的表中列出。 In practice, users generally search for people who have "at least knowledge level X in skill Y". 在实践中,用户通常搜索具有“至少技能Y的知识水平X”的人员。 Is there a module or straightforward way to display the allowed values as a drop-down and use a "greater than" operator in the query instead of a "one of"? 是否有模块或直接方法将允许的值显示为下拉列表,并在查询中使用“大于”运算符而不是“其中之一”?

Any sample code or advice on how to dynamically change the filter logic or the WHERE clause of the query would be very appreciated. 任何有关如何动态更改查询的过滤器逻辑或WHERE子句的示例代码或建议,将不胜感激。

You want to use hook_views_query_alter() , while I haven't specifically altered the WHERE clause, I have altered the SORTBY clause and the idea behind both should be relatively similar. 您想使用hook_views_query_alter() ,虽然我没有专门更改WHERE子句,但已经更改了SORTBY子句,并且两者的思想应该相对相似。

Here's a quick piece of code: 这是一段简短的代码:

function my_module_views_query_alter(&$view, &$query) {
  switch ($view->name) {
    case 'view1':
      $args = _my_module_get_querystring();
      switch ($args['condition']) {
        case 'condition1':
          $query->where[0]['args'][0] = 1;
          break;

        case 'condition2':
          $query->where[0]['args'][0] = 2;
          break;
      }
      break;
  }
}

/**
 * Returns querystring as an array.
 */
function _my_module_get_querystring() {
  $string = drupal_query_string_encode($_REQUEST, array_merge(array('q'), array_keys($_COOKIE)));
  $args = explode('&', $string);
  foreach ($args as $id => $string) {
    unset($args[$id]);
    $string = explode('=', $string);
    $args[$string[0]] = str_replace(' ', '-', $string[1]);
  }
  return $args;
}

This particular piece would allow you to alter the WHERE clause using a querystring (?condition=condition1), but you could alter it to get the arguments however you wish. 这个特殊的片段将允许您使用查询字符串(?condition = condition1)更改WHERE子句,但是您可以更改它以获取所需的参数。

Hope this helps. 希望这可以帮助。

Using Decipher's sample and spending a few hours reading up and playing around, I've gotten a basic module that works perfectly for my needs. 使用Decipher的样本并花费几个小时阅读和玩耍,我已经获得了一个基本模块,可以很好地满足我的需求。 Thanks again! 再次感谢!

Here's my code if anyone else comes across a similar need: 如果有人遇到类似的需求,这是我的代码:

<?php
// $Id$
/**
* @file
* Module for modifying the views query to change an EQUALS 
* to a GREATER THAN for specific filters.
*/


function views_greater_than_views_query_alter(&$view, &$query) {

//only implement for views that have Search in their name
    if(strstr($view->name, "search")) {

        $whereclauses = $query->where[0]['clauses'];

        foreach ($whereclauses as $i=>$currentrow) {

            $currentrow = str_replace('= %d', '>= %d', $currentrow);    
            $query->where[0]['clauses'][$i] = $currentrow;
    }

    unset($whereclauses);
    }
}

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

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