Drupal 在Views 中自定义筛选 Filter

PHPABC Drupal 936 次浏览 , , 没有评论

在Views中设定了一 个Node,Node中有个field_warehousefactory  字段通过Tax取到数据,User中有个Field_warehousefactoryid ,想法是当前登录用户在查看此Views时只显示User->Field_warehousefactoryid =node->field_warehousefactory 的文档,但通过Views内置的筛选没办法同时取到二个值,通过搜索和QQ群中的讨论最后找到了一个通过hook_views_query_alter 进行修改Views的SQL来操作。

下面直接把过记录下来,先设置好Page。
drupal设置Page

在模块中写个自己的小模块:

    <?php  
    //hook_views_query_alter  
    function views_warehouse_factoryfilter_views_query_alter(&$view, &$query) {  
 
                if($view->name=="warehouseviews"&& $view->current_display == "page"){  
                $join=new views_join();  
                $join->table='field_data_field_warehousefactory';  
                $join->field='entity_id';  
                $join->left_table='node';  
                $join->left_field='nid';  
                $join->type='inner';  
                $join->extra=array(0 => Array ( 'field' => "entity_type" ,  
                                                'value' => "node" ),   
                                   1 => Array ( 'field' => "deleted",   
                                                'value' => 0 ,  
                                                'numeric' => 1 ) );  
                $join->definition=array('left_field' => "nid" ,  
                                        'field' => "entity_id" ,  
                                        'extra' => Array ( 0 => Array ( 'field' => "entity_type", 'value' => "node" ) ,  
                                                           1 => Array ( 'field' => "deleted", 'value' => 0 ,'numeric' => 1 ) ),   
                                        'table' => "field_data_field_warehousereturndate" ,  
                                        'left_table' => "node");  
                $join->entity_type='and';  
                $join->adjusted=1;  
 
                $query->table_queue['field_data_field_warehousefactory']=array(  
 
                            'table'=>"field_data_field_warehousefactory",  
                            'num'=>"1",  
                            'join'=>$join,  
                            'alias'=>"field_data_field_warehousefactory",                          
                            'relationship'=>"node");  
 
                $query->where[1]['conditions'][2]=array(  
                        'field'=>"field_data_field_warehousefactory.field_warehousefactory_tid",  
                        'value'=>'91',  
                        'operator'=>"in"  
                        );            
            dpm($query);  
            }  
        }

通过dpm($query);之后可以看到相关增加的自定义内容。
dpm($query)

但在发现了报错:
Drupal报错
在通过查看Views SQL发现了明确的问题。
View SQL

My Gold 在Where的In中没有 91 这个值。
 
通过不断的测试和开放思想,最后发现了单值使用“=”进行赋值,多值使用的IN 就需要用array数组进行赋值。现在修改下为:
 
[php] view plain copy
在CODE上查看代码片派生到我的代码片
 
    <?php  
    //hook_views_query_alter  
    function views_warehouse_factoryfilter_views_query_alter(&$view, &$query) {  
 
        //if($view->name=="warehouseviews"&& $view->current_display == "page"){  
                //print_r($query);  
                $join=new views_join();  
                $join->table='field_data_field_warehousefactory';  
                $join->field='entity_id';  
                $join->left_table='node';  
                $join->left_field='nid';  
                $join->type='INNER';  
                $join->extra="(field_data_field_warehousefactory.entity_type = 'node' AND  field_data_field_warehousefactory.deleted = '0')";      
                $join->entity_type='AND';  
                $join->adjusted=1;  
 
                $query->table_queue['field_data_field_warehousefactory']=array(  
 
                            'table'=>"field_data_field_warehousefactory",  
                            'num'=>"1",  
                            'join'=>$join,  
                            'alias'=>"field_data_field_warehousefactory",                          
                            'relationship'=>"node");  
 
                $query->where[1]['conditions'][]=array(                
                        'field'=>"field_data_field_warehousefactory.field_warehousefactory_tid",  
                        'value'=>array('90','91'),  
                        'operator'=>"IN"  
                        );  
                //单值赋值使用“=”,多值赋值使用 array   
                //$query->where[1]['conditions']=array(                
                //      'field'=>"field_data_field_warehousefactory.field_warehousefactory_tid",  
                //      'value'=>'91',  
                //      'operator'=>"=");  
                //dpm($query);  
        //}  
        }

最后实现到程序中的修改结果为:

    <?php  
    //hook_views_query_alter  
    function views_warehouse_factoryfilter_views_query_alter(&$view, &$query) {  
            if($view->name=="warehouseviews"){  
 
                $join=new views_join();  
                $join->table='field_data_field_warehousefactory';  
                $join->field='entity_id';  
                $join->left_table='node';  
                $join->left_field='nid';  
                $join->type='INNER';  
                $join->extra="field_data_field_warehousefactory.entity_type = 'node' AND  field_data_field_warehousefactory.deleted = '0'";    
                $join->entity_type='AND';  
                $join->adjusted=1;  
 
                $query->table_queue['field_data_field_warehousefactory']=array(  
 
                            'table'=>"field_data_field_warehousefactory",  
                            'num'=>"1",  
                            'join'=>$join,  
                            'alias'=>"field_data_field_warehousefactory",                          
                            'relationship'=>"node");  
                global $user;  
                $user_load=user_load($user->uid); //取得用户扩展的字段  
                $factoryid=$user_load->field_factory_location['und']; //取得用户所在工厂的数组  
                $arr_factory=array();  
                foreach($factoryid as $factory){                  
                    $arr_factory[]=$factory['tid']; //对用户所在工厂数组进行适配到$query->where数组的Value中去。                           
                }             
                if(count($arr_factory)>1){             
                    $query->where[1]['conditions'][]=array(                
                        'field'=>"field_data_field_warehousefactory.field_warehousefactory_tid",                   
                        'value'=>$arr_factory,                     
                        'operator'=>"IN"  
                        );  
                        }  
                else {  
                    $query->where[1]['conditions'][]=array(                
                        'field'=>"field_data_field_warehousefactory.field_warehousefactory_tid",  
                        'value'=>$arr_factory[0],  
                        'operator'=>"=");                      
                }   
                //单值赋值使用“=”,多值赋值使用 array              
                dpm($query);  
        }  
        }

发表评论

电子邮件地址不会被公开。 必填项已用*标注

Go