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的数组元素你就会一目了然了,希望可以帮到大家。
注:上面的例子是我用来将时间日期转换为时间戳的,当然你可以在这里任意的改动

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download
The most popular open source editor
