Drupal 6 Views 2: Setting Date Arguments

删除回忆录丶 提交于 2019-12-03 22:44:33

问题


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

回答1:


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




回答2:


If you're using views 2, you can use the GUI to add a date argument. Then in the url, you can put :

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

For the startDate/finishDate, the format is YYYY-MM-DD-HH.

GL!



来源:https://stackoverflow.com/questions/1301138/drupal-6-views-2-setting-date-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!