Question

I'm using drupal 6 and view 2 -

I've created a custom filter for my view using this documentation

http://www.chadcf.com/blog/creating-custom-filters-drupal-and-views.

Every things seems to working fine only one problem -

My pagination not working (not appearing with some operator)

Let me explain what i want to achieve in brief -

In my page view i already have some exposed filter with their operators dropdown and i created one custom exposed filter without operator drop down.

My requirement is to merge the one existing filter functionality with my custom filter. Means two filter will be working together to get to desired result. But one filter is consist of existing field and if i choose any of it's operator from drop down then it's value is included in where clause, which i don't want. So i completely removed chunk of query from view query by using

hook_views_pre_execute(&$view) {
        $view->build_info['query'] = preg_replace('/AND \(node_data_field_stock.field_stock_value [<<=>>=!=\\s%d|IS NULL|IS NOT NULL]*\)/','',$view->build_info['query']);
      } 

Now in my sites/all/modules/mymodule/inc/mymodule_filter.inc

function query() {
        $this->query->add_where($this->options['group'], "MY_QUERY");   
}

which add where clause to view query.

Using above procedure i was successfully extended the default view behaviour and also getting the desired result but for some operator pagination link not appearing even i know that there are more records exist in database.

I know why it's happening - because my view pagination script not aware of the changes i've made.

My Question is HOW CAN I OVERRIDE VIEW PAGER QUERY?

Any help would be greatly appreciated.

Was it helpful?

Solution

What i was doing to remove the where clause from view query is below.

hook_views_pre_execute(&$view) {
           $view->build_info['query'] = preg_replace('/AND \(node_data_field_stock.field_stock_value [<<=>>=!=\\s%d|IS NULL|IS NOT NULL]*\)/','',$view->build_info['query']);
    } 

But the proper place to remove the where clause you want to remove from view query is hook_views_query_alter(). By using this tutorial

http://www.drupaler.co.uk/blog/altering-views-query-tackling-node-type-filter-bug/451

    function hook_views_query_alter(&$view, &$query) {
        if($view->name == 'view_name'){
            var_dump($query->where[0]['clauses']);
                 foreach ($query->where[0]['clauses'] as $key=>$value) {
                    if(preg_match('/node_data_field_stock.field_stock_value/',$value)){
                        unset($query->where[0]['clauses'][$key]);
                       // remove the where clause by it's key
                    }
                 }

            var_dump($query->where[0]['clauses']);
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top