Drupal 6 视图 2:设置日期参数 [英] Drupal 6 Views 2: Setting Date Arguments
问题描述
将 uid 作为参数传递适用于以下代码:
Passing uid as an argument works fine with this code:
$bouts = views_get_view_result('Results', 'page_1', array($user->uid));
views_get_view_result 中设置参数的关键行是:
The key line in views_get_view_result that sets arguments is:
$view->set_arguments($args);
但是传递日期范围呢?
此外,如果某些内容被指定为视图上的过滤器,是否可以通过编程方式更改它?
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));
}
}
推荐答案
至于传递数据范围并给出已发布的函数定义,只有当视图接受它们作为参数时,您才能将日期范围传递给该范围.我不是 100% 确定,但 afaik 日期范围只能定义为过滤器,不能定义为参数,这会导致您的第二个问题:
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);
之后,$view 包含整个视图对象.过滤器设置是按显示器定义的,因此假设您有一个只有默认显示器的视图,您将在
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']
(注意对象/数组符号的混合——显示是一个views_display类型的包含对象)
(Note the object/array notation mix - the display is a contained object of type views_display)
'filters' 数组每个过滤器包含一个条目,根据过滤器类型具有不同的元素.为了您的目的,我建议仅使用您感兴趣的过滤器和预配置/硬编码值创建一个虚拟视图.使用调试器(或 var_dump
/print_r
),您可以在创建视图后查看过滤器数组.根据您在那里找到的内容,您应该能够推断出如何注入您的自定义日期范围.
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.
免责声明:像这样在视图中四处闲逛有点烦人且效果不佳,但它确实有效.到目前为止,我还没有找到可以直接解释内部结构的 Views2 简明文档,因为我发现 官方 API 文档 对代码的用法有点缺乏.(当然,这很可能只是我愚蠢;)
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屋!