2

I am working on a module to collect the results from several views, to be concatenated and then displayed. The views are returning JSON data using the views_datasource module.

To do this, I tried to set a variable to be the output of a view like so:

$my_view = views_get_view('drupalgap_content');
$my_view->set_display('getlatestdigest');
$output = $my_view->render();

Then I could concatenate the $output to another views output etc.

The problem lies in the fact that as soon as I call $view->render() it renders the view results to the screen (I presume with a print() function) somewhere deep in the views core module.

After a ton of googling, I found that in the past, $view->render() used to return a value, and this method was correct. However, somewhere along the line, they changed it so $view->render() it renders the view results to the screen, making it impossible to concatenate the view output to another view output.

Other options I've looked into that don’t work:

  1. $view_result = views_get_view_result('drupalgap_content','getlatestdigest');

    This gets the view result, but it does not get the rendered JSON output that I need from the views_datasource module. So, it’s no good.

  2. $view-execute(),$view->preview('getlatestdigest'); $view->execute_display('getlatestdigest', array());

    All of these three functions do the same thing as view->render(), i.e. they output the nicely formatted results I want to the screen instead of assigning them to a variable.

  3. views_embed_view('drupalgap_content', ‘getlatestdigest');

    does the same thing as view->render(), i.e. outputs the nicely formatted results I want to the screen instead of assigning them to a variable.

I should also mention that I am calling this module’s function using an AJAX call from a mobile app to the menu_hook for this page_callback. So I can not have it outputting anything to the “screen” until it is ready since AJAX will return the data to the client as soon as the output to the screen is called.

1

This turned out to be an issue with the views_datasource module, not the views module. Solved by enabling "Views API mode" in the View settings.

  • I was exactly in the same situation; this solved the issue perfectly, thank you! – Gabriele Apr 4 '18 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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