2009-08-19 36 views
1

传递UID作为参数正常工作与此代码:Drupal的6次2:设置日期参数

$bouts = views_get_view_result('Results', 'page_1', array($user->uid)); 

在views_get_view_result重点线,设置参数为:

$view->set_arguments($args); 

但是关于通过什么日期范围?

另外,如果在视图中指定了某种过滤器,是否有一种方法可以对它进行参数化修改?

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)); 
    } 
} 
+0

AFAIK views模块中没有'views_get_view_result()'函数 - 你自己声明了吗?如果是这样,你应该添加它的定义以允许答案。 – 2009-08-23 17:43:28

+0

你已经创建了如此多的drupal问题。你应该把所有的东西合而为一。 – 2009-08-29 03:45:20

回答

1

作为将数据范围并给予发布函数定义,你可以通过日期范围中,只有当该视图会接受它们作为参数。我不是100%确定,但afaik日期范围只能被定义为过滤器,而不是作为参数,导致你的第二个问题:

以编程方式更改视图过滤器设置是可能的,但有点混乱,给定相当复杂的视图对象/数组mashup结构。在你上面发布的功能中,第一行是

$view = views_get_view($viewname); 

之后,$ view包含整个视图对象。所述过滤器设置每个显示定义的,因此假设你有一个视图仅具有默认显示,你会发现

$view->display['default']->display_options['filters'] 

下在过滤器设置(注意,对象/数组符号组合 - 的显示是一个包含的对象类型views_display)

'filters'数组每个过滤器都包含一个条目,根据过滤器类型不同而有不同的元素。为了您的目的,我建议仅使用您感兴趣的过滤器和预配置/硬编码值创建一个虚拟视图。使用调试器(或var_dump/print_r),您可以在创建视图后查看过滤器数组。从你在那里找到的,你应该能够推断出如何注入你的自定义日期范围。


免责声明:在这个视图中查看这样有点烦人,而不是有效,但它的工作原理。到目前为止,我还没有找到简明的Views2文档,可以直接解释这些内容,因为我发现official API documentation对代码的使用有点缺乏。 (当然,这可能只是我愚蠢;)

0

如果您使用视图2,您可以使用GUI来添加日期参数。然后,在URL,就可以把:

www.yousite.com/yourview/startDate--finishDate

对于起始日期/ finishDate,格式为YYYY-MM-DD-HH。

GL!