Home > Backend Development > PHP Tutorial > Drupal7 modifies (adds) the query conditions of the view_PHP tutorial

Drupal7 modifies (adds) the query conditions of the view_PHP tutorial

WBOY
Release: 2016-07-13 17:54:27
Original
1059 people have browsed it

Although drupal’s view is easy to use, in some cases we want to dynamically modify the query conditions according to our own needs. This is not an exaggeration. Then I will tell you a good way to modify the query conditions of the view. At the same time, you can also add legal query conditions according to your needs


​ 1. In your module you need to declare a hook like this

 
[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 query conditions of the view. It uses a hook and a custom method

[php]
function modulename_views_query_alter(&$view, &$query) {
If ($view->name == 'viewname') {
                     $data = _get_views_operationinfo_time_key($query->where[1]['conditions']);
If (count($data) > 0) {
foreach ($data as $d) {
$query->where[1]['conditions'][$d]['value'] = strtotime($query->where[1]['conditions'][$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 meaning of the above characters:
1. modulename Your module name

2. viewname The machine-readable name of your view
3. fieldname The name of the field to which conditions are to be added. Here it is specified by table name.field name

If you have any questions, please leave a message. In the above code, if you print out the large array $query and find the array element with the subscript where, you will know it clearly. I hope it can help everyone.

Note: The above example is what I used to convert time and date into timestamp. Of course, you can make any changes here

http://www.bkjia.com/PHPjc/477964.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477964.htmlTechArticleAlthough drupal’s view is easy to use, in some cases we want to dynamically modify the query conditions according to our own needs. This is not an exaggeration, so let me tell you a good way to fix it...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template