Is there a way to retrieve pagination information in the views-services result? Right now I get a list of JSON dictionaries and the limit is based on the pager that I have implemented in the view. But I'd like to have a "pager" dictionary that provides the total number of available results. The reason is that building a front-end with "more", "previous" and "next" links requires that I know the total number of entries.

I did not find a way to get this information in any of the documentation. If I need to implement something like this I wonder if I could get some potential pointers on how to get this information and how to format the result.

Comments

cybertoast’s picture

I guess I could hard-code hard-code the total into the rendered response. Eg.:

function _get_view_total_rows($view_name, $display_id, $args, $filters) {
  // Statistics and counts

  $view_stat = views_get_view($view_name);
  $view_stat->set_display($display_id);
  $view_stat->set_arguments($args, FALSE);
  $view_stat->set_exposed_input($filters);
  $view_stat->pre_execute();
  $view_stat->execute();
  $total_rows = $view_stat->total_rows;

  return $total_rows;
}
function _set_total_rows(&$item, $total_rows) {
  // Add stats into each row if requested
  $total_rows_label = "_total_rows";
  $item->$total_rows_label = $total_rows;
}

Then insert the total into the item object:

_set_total_rows(&$item, $total_rows);

But this seems extremely un-drupal-esque and probably has horrendous unintended consequences. What would potentially be better is if this was configurable via settings for this module, where the label could be set if this stat is to be enabled.

I wonder if anyone with experience with development on services-views could suggest some possible best-practice paths.

ygerasimov’s picture

Status: Active » Fixed
StatusFileSize
new1.22 KB

I have added a hook to alter views results before sending them to services for rendering. Please see services_views.api.php for example.

cybertoast’s picture

Category: support » feature
Status: Fixed » Patch (to be ported)
StatusFileSize
new1.17 KB

Thanks much, this is much cleaner. The issue though is that the total_rows seem to only be retrievable by setting $view->get_total_rows = TRUE, per http://drupal.org/node/1306196. What do you think about the attached patch (to 7.x.1.x)?

Thanks again - taught me something about drupal's stack development.

ygerasimov’s picture

Status: Patch (to be ported) » Fixed

I think you can set $view->get_total_rows = TRUE; in one of views hooks. I would try hook_views_pre_build(). So no need services_views to interfere this variable in "case" of "hook_alter tries to get $view->total_rows".

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

bigwebfoundry’s picture

Issue summary: View changes

I am not able to understand #2, #3, #4.
Please help me where do I need to put this code $view->get_total_rows = TRUE; to output pager informat(count, total rows) in response json , I tried like follwing:

function mymodule_services_views_execute_view_alter(&$output, $view) {
  if ($view->name == 'articles_response') {
    $view->get_total_rows = TRUE;
    $paged_output = array(
      'results' => $output,
      'total_rows' => $view->total_rows,
    );
    $output = $paged_output;
  }
}

But it didn't work,
I want something like this https://www.drupal.org/node/2301749 but code there is not working with views 3
Please help

derekwebb1’s picture

So what is the variable or parameter to get the services_views module to output the view metadata with the result. I tried to use the hook_services_views_execute_view_alter(&$output, $view) and found that it did nothing at all. From what I can tell from adding watchdog logs, it doesn't even get called when requesting a view via services_views... What does one have to do to get metadata? Any guidance is appreciated.

I used the following and there was no change:

function myModule_services_views_execute_view_alter(&$output, $view) {
  $paged_output = array(
    'results' => $output,
    'total_rows' => $view->total_rows,
  );
  $output = $paged_output;
}

I also found that watchdog logs placed in services_views_execute_view, where the alter is initiated, are not called at all.

derekwebb1’s picture

Status: Closed (fixed) » Active
derekwebb1’s picture

I had to create a new service to load pagination info after the services_views call was made. It would be nice if pagination info was included in the returned results.

derekwebb1’s picture

I also tried the alternate path to getting a service with this module: using the individual view service (where you select the view in the services admin page as the resource)

On the plus side, the pagination information as refered to in the api finally showed up. The bad news is no arguments whatsoever are honored, so all of the following fail:

http://mysite.com/myview-service/myview?arg[0]=limit=1
http://mysite.com/myview-service/myview?limit=1
http://mysite.com/myview-service/myview?tid=1
http://mysite.com/myview-service/myview?filters[tid]=1

No arguments are honored. Any suggestions on what I might be doing wrong would be nice. Thanks

fmoutawe’s picture

Hello,

I have the same problem with pagination and my_module_services_views_execute_view_alter hook is not the solution.

This hook allow to modify only the preview (when you are in B.O) !

However, services_views_retrieve() is used to send result to the endpoint.

I will try to find a good solution.

mhmd’s picture

StatusFileSize
new116.44 KB

In order to make pagination work with services views which comes when you make a services display with path to access like

{endpoint}/{services display path} NOT FROM {endpoint}/views/{view name}

You have to configure EXPOSED OPTIONS from Pager settings link like the image attached

After that you must set ?offset=0&items_per_page=5 after you endpoint url like

{endpoint}/{services display path}?offset=0&items_per_page=5

NOTE : items per page must be like 5,10,20,50,100 and include items to display value.

Only local images are allowed.

rasikap’s picture

You can enable the views php and write the following code :
if (!isset($static['size'])) {
foreach($view->result as $each_result) {
$result_array[] = $each_result->nid;
}
$size = count($result_array);
$static['size'] = $size;
}
return $static['size'];

This will return the total number of counts of result.

Another approach is to to user views_query_alter to get the total number of records and pass in the result array.

rjdjohnston’s picture

Checkout this article:
http://www.christophermchurch.com/services-views-api-add-on-module/

And this custom module made by Chris:
https://github.com/cmchurch/drupal_services-views-totals

Works beautifully!!

eternallight’s picture

And this custom module made by Chris:
https://github.com/cmchurch/drupal_services-views-totals

Awesome, thanks!