10

I'd like to programmatically execute a view (providing arguments) and get the raw results, without rendering it. Is it possible?

When I use $view->preview('default', $args); it converts the results to HTML. I know I can use $view->result to iterate the raw results, but the custom rendered I'm using for some fields are buggy and give me a WSOD (I have no interest or ability to fix the renderer, it's an edge case :) )

I'm using Views 3 with Drupal 7

1

3 Answers 3

11

There is already a function that does exactly what you want:

  $result = views_get_view_result('my_view_name', 'default', $arg1, $arg2, ...);

Read more at its page at api.drupal.org:

https://api.drupal.org/api/views/views.module/function/views_get_view_result/7

4

$view->preview() is generally used to simply view the display so in other words it will render it via the specified format.

$view->execute_display() also returns the display in relevant format specified in view.

I think you may have to result to executing query direct i.e. db_query()

1
  • I guess I'll just have to resort to building the query myself with db_query. Thanks for helping :)
    – Daniel
    Aug 21, 2011 at 18:42
2

You should get some success if you stub out view::preview() into your own code, but change it so that, instead of calling ->display_handler->preview(), it calls ->execute() instead:

$view = views_get_view($view_name);
$view->set_display($display_name);
$view->preview = TRUE;
$view->pre_execute($args);
$view->execute($display_name);

This should work because, for most display handlers, the call to ...->display_handler->preview() is actually just a wrapper around a call back to view::render(). Which itself starts by calling $this->execute(...).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.