Drupal7 modify (ADD) view query Conditions

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.