繁体   English   中英

Drupal 6视图2:设置日期参数

[英]Drupal 6 Views 2: Setting Date Arguments

将uid作为参数传递可以在以下代码中正常工作:

$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));
  }
}

至于传递数据范围并给定发布的函数定义,只有在视图接受它们作为参数的情况下,才可以将日期范围传递给该范围。 我不确定100%,但是afaik日期范围只能定义为过滤器,而不能定义为参数,这会引出您的第二个问题:

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

$view = views_get_view($viewname);

之后,$ view包含整个视图对象。 过滤器设置是针对每个显示定义的,因此,假设您的视图只有默认显示,您将在下面找到过滤器设置

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

(请注意,对象/数组表示法混合-显示是views_display类型的包含对象)

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


免责声明:在这样的视图中四处逛逛有点烦人,虽然效果不佳,但它确实有效。 到目前为止,我还没有找到一个简洁的Views2文档来直接解释这些内脏,因为我发现官方API文档在代码使用方面有些欠缺。 (当然,这很可能只是我愚蠢;)

如果使用的是视图2,则可以使用GUI添加日期参数。 然后在网址中,您可以输入:

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

对于startDate / finishDate,格式为YYYY-MM-DD-HH。

GL!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM