views:

2963

answers:

1

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  A: 

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

Henrik Opel