Home > Article > Backend Development > Drupal7 modifies (adds) the query conditions of the view_PHP tutorial
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.
http://www.bkjia.com/PHPjc/477964.html