Drupal 视图中公开过滤器的自定义逻辑

发布于 2024-08-20 20:25:29 字数 337 浏览 4 评论 0原文

我正在开发一个 Drupal 网站,希望得到一些关于这方面的建议。目前,用户输入他的多种不同技能的等级。它存储在 CCK 整数字段中,并作为包含键/值对 1|Beginner、2|Intermediate、3|Advanced 的下拉小部件向用户公开。

在视图中,我公开了每个技能的允许值,这些值以复选框的形式呈现给用户(使用“更好的公开过滤器”模块),然后在可排序的表中列出。在实践中,用户通常会搜索“在技能 Y 上至少具有 X 知识水平”的人。是否有模块或简单的方法将允许的值显示为下拉列表并在查询中使用“大于”运算符而不是“其中之一”?

任何有关如何动态更改过滤器逻辑或查询的 WHERE 子句的示例代码或建议将非常感激。

I'm working on a Drupal site and would love some advice on this. 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.

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". 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谎言 2024-08-27 20:25:29

您想使用 hook_views_query_alter(),而我没有专门更改 WHERE 子句,我修改了 SORTBY 子句,两者背后的想法应该相对相似。

这是一段快速的代码:

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;
}

这段特定的代码允许您使用查询字符串 (?condition=condition1) 更改 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.

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.

Hope this helps.

梦归所梦 2024-08-27 20:25:29

使用 Decipher 的示例并花费几个小时阅读和使用,我得到了一个完全满足我的需求的基本模块。再次感谢!

如果其他人遇到类似的需求,这是我的代码:

<?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);
    }
}

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. 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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文