Drupal的6次2:设置日期参数 [英] Drupal 6 Views 2: Setting Date Arguments
问题描述
传递UID作为参数正常工作与此code:
$较量= views_get_view_result('结果','PAGE_1,阵列($用户可> UID));
在views_get_view_result重点线,设置参数是:
$视图 - > set_arguments($参数);
但是关于传递日期范围是什么?
另外,如果事情被指定为在视图上的过滤器,是有办法prorammatically改变它?
views_get_view_result:/ **
*调查一个视图的结果。
*从Drupal.org。
*
* @参数字符串$视图名
*视图的名称从检索数据。
* @参数字符串$ display_id
*显示器ID。对有问题的视图编辑页面,你会发现
*在控制区的左侧显示的列表。 默认值
*将在该列表的顶部。将光标悬停的名字
*显示您要使用。这样的URL将出现在状态栏上的
*浏览器。这通常是在窗口的底部,在铬。
*#后的意见,制表一切都显示ID,例如PAGE_1。
* @参数数组的$ args
*参数数组。 (没有钥匙,只是参数)
* @返回
*阵
*包含每个视图项的对象的数组。
*字符串
*如果没有找到该视图被返回的消息。
* /
功能views_get_view_result($视图名,$ display_id = NULL,的$ args = NULL){
$视图= views_get_view($视图名);
如果(is_object($视图)){
如果(is_array($参数)){
$视图 - > set_arguments($参数);
}
如果(IS_STRING($ display_id)){
$视图 - > set_display($ display_id);
}
其他{
$视图 - > init_display();
}
$视图 - > pre_execute();
$视图 - >执行();
/ *打印< pre> $视图名:$ display_id
的print_r(get_class_method(0函数$视图)); * /
返回$视图 - >的结果;
}
其他{
返回T('查看%视图名没有找到。,阵列(%视图名'=> $视图名));
}
}
对于通过数据范围,并给予张贴的函数定义,你可以传递日期范围为,只有当该视图会接受它们作为参数。我不是100%肯定,但据我所知日期范围只能被定义为过滤器,不作为参数,从而导致你的第二个问题:
编程改变看法的过滤器设置是可能的,但有点凌乱,考虑到相当复杂的视图对象/数组混搭结构。在上面贴的功能,第一行是
$视图= views_get_view(视图名$);
之后,$视图包含整个视图对象。该过滤器设置每个显示器定义的,所以假设你有一个观点,只有一个默认显示,你会在
找到过滤器设置 $视图 - >显示['默认'] - > display_options ['过滤器']
(注意对象/数组符号组合 - 显示器的类型views_display的包含对象)
过滤器数组包含过滤器每一个条目,以根据不同的过滤器类型不同的元素。你的目的,我建议创建一个虚拟视图只有你感兴趣的过滤器,以preconfigured /硬codeD值。使用调试器(或的var_dump
/ 的print_r
),然后你可以看看视图创建后滤波器阵列。从你找到那里,你应该能够推断出如何注入您的自定义日期范围。
免责声明:像这样的观点四处是有点讨厌,而不是有效的,但它的作品。作为然而,我还没有发现Views2的简明文档可以解释在一条直线前进的道路内脏,因为我找到的官方的API文档有点欠缺就从code中的用法。 (当然,这很可能只是我是愚蠢;)
Passing uid as an argument works fine with this code:
$bouts = views_get_view_result('Results', 'page_1', array($user->uid));
The key line in views_get_view_result that sets arguments is:
$view->set_arguments($args);
But what about passing date ranges?
Also, if something is specified as a filter on a view, is there a way to prorammatically alter it?
views_get_view_result:
/**
* Investigate the result of a view.
* from Drupal.org.
*
* @param string $viewname
* The name of the view to retrieve the data from.
* @param string $display_id
* The display id. On the edit page for the view in question, you'll find
* a list of displays at the left side of the control area. "Defaults"
* will be at the top of that list. Hover your cursor over the name of the
* display you want to use. A URL will appear in the status bar of your
* browser. This is usually at the bottom of the window, in the chrome.
* Everything after #views-tab- is the display ID, e.g. page_1.
* @param array $args
* Array of arguments. (no keys, just args)
* @return
* array
* An array containing an object for each view item.
* string
* If the view is not found a message is returned.
*/
function views_get_view_result($viewname, $display_id = NULL, $args = NULL) {
$view = views_get_view($viewname);
if (is_object($view)) {
if (is_array($args)) {
$view->set_arguments($args);
}
if (is_string($display_id)) {
$view->set_display($display_id);
}
else {
$view->init_display();
}
$view->pre_execute();
$view->execute();
/* print "<pre> $viewname: $display_id";
print_r(get_class_methods($view)); */
return $view->result;
}
else {
return t('View %viewname not found.', array('%viewname' => $viewname));
}
}
As for passing data ranges and given the posted function definition, you could pass date ranges to that only if the view would accept them as arguments. I'm not 100% sure, but afaik date ranges can only be defined as filters, not as arguments, which leads to your second Question:
Programmatically altering the views filter settings is possible, but a bit messy, given the rather complicated view object/array mashup structure. In your posted function above, the first line is
$view = views_get_view($viewname);
After that, $view contains the whole view object. The filter settings are defined per display, so assuming you have a view with only a default display, you will find the filter settings under
$view->display['default']->display_options['filters']
(Note the object/array notation mix - the display is a contained object of type views_display)
The 'filters' array contains one entry per filter, with varying elements depending on the filter type. For your purpose, I would suggest to create a dummy view with just the filter you are interested in, with preconfigured/hardcoded values. Using a debugger (or var_dump
/print_r
) you can then take a look at the filter array after view creation. From what you find there, you should be able to deduce how to inject your custom date range.
Disclaimer: Poking around in the view like this is a bit annoying and not to effective, but it works. As of yet, I have not found a concise documentation of Views2 that would explain the innards in a straight forward way, as I find the official API documentation a bit lacking concerning the usage from code. (Of course this could well be just me being to stupid ;)
这篇关于Drupal的6次2:设置日期参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!