开发者

Custom logic for exposed filters in a Drupal view

开发者 https://www.devze.com 2022-12-19 07:03 出处:网络
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

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 th开发者_运维百科an" 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.


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号