Although drupal's view is easy to use, in some cases, we want to dynamically modify the query conditions based on our own needs. This is not enough, next, we will show you a good way to modify the query conditions of the view. You can also add valid query conditions as needed.
1. In your module, You need to declare a hook
[Php]
Function modulename_views_api (){
Return array (
'API' => 3,
'Path' => drupal_get_path ('module', 'modulename ').'/',
);
2. Create a file (modulename. views. inc) under your Module Directory to modify the view query conditions. A hook and a custom method are used in it.
[Php]
Function modulename_views_query_alter (& $ view, & $ query ){
If ($ view-> name = 'viewname '){
$ Data = _ get_views_operationinfo_time_key ($ query-> where [1] ['condition']);
If (count ($ data)> 0 ){
Foreach ($ data as $ d ){
$ Query-> where [1] ['conditions'] [$ d] ['value'] = strtotime ($ query-> where [1] ['condition'] [$ d] ['value']);
}
}
}
}
Function _ get_views_operationinfo_time_key ($ conditions ){
$ Data = array ();
Foreach ($ conditions as $ key => $ val ){
If ($ val ['field'] = 'fieldname '){
$ Data [] = $ key;
}
}
Return $ data;
}
The meanings of the above characters:
1. modulename your module name
2. viewname your view's machine read name
3. fieldname: name of the field to be added with conditions. It is specified by the table name and field name.
If you have any questions, please leave a message. If you print out the large array $ query and find the array element whose subscript is where, you will see it at a Glance. I hope this will help you.
Note: The above example is used to convert the time and date to the timestamp. Of course, you can modify it at will.