drupal7修改(添加)view 的查询条件_PHP教程

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

drupal的view虽然好使但是在有些情况下我们想根据自己的需求来动态的修改查询条件,这不足为过,那么接下来就告诉你一个很好的方法来修改view的查询条件,同时你也可以根据需求来添加查询合法的查询条件


1、在你的模块中你要声明一个这样和钩子


[php]
function modulename_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'modulename') . '/',
);

2、在你的模块目录下面创建一个文件(modulename.views.inc),用来修改view的查询条件 ,里面用到一个钩子,和一个自定义方法

[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;
}

以上几个字符的意思:
1、modulename 你的模块名字

2、viewname 你的view的机读名字
3、fieldname 要加条件的字段的名字,这里它是以表名.字段名来规定的

如果疑问请留言,上面的代码如果你把$query这个大数组打印出来找到下标为where的数组元素你就会一目了然了,希望可以帮到大家。

注:上面的例子是我用来将时间日期转换为时间戳的,当然你可以在这里任意的改动

www.bkjia.com true http://www.bkjia.com/PHPjc/477964.html TechArticle drupal的view虽然好使但是在有些情况下我们想根据自己的需求来动态的修改查询条件,这不足为过,那么接下来就告诉你一个很好的方法来修...
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!