3

Background

I'm having a go at porting the JavaScript random views module from 6 to 7.

Problem

One of functions I think is intended to override the default pager value to load extra rows per page. (i.e. pager says load 8. This overrides it and says load 20).

To accomplish this it uses hook_views_pre_execute

function views_javascript_random_views_pre_execute(&$view) {
  .....

  // Here it saves the current settings of the pager for use later
  .....

  // Set new settings
  $view->set_items_per_page($sorts['js_random']['client_node_count']);
}

Unfortunately the set_items_per_page function doesn't seem to work. While it still sets:

$view->items_per_page

This then seems to have no effect on how many rows are then loaded in:

function views_javascript_random_preprocess (&$vars, $hook) {
   ....
   dpm($vars);
} 

In $vars->rows, it still only loads the number of rows set by the pager (i.e. 8 not 20)

The API function definitely still works in 7. Could someone explain why it's not working correctly in this case?

2

I have successfully used $view->set_items_per_page() in hook_views_pre_build(). The problem might be that in hook_views_pre_execute() the pager settings have already been built, so you might have to update those as well, with something like $view->display_handler->get_plugin('pager')->get_items_per_page().

  • So in this case I was able to switch over to hook_views_pre_build() and it worked fine. (All the other information I needed was already present in views_pre_build.) I guessing it is something do with the pager already being built. – Dominic Woodman Oct 2 '14 at 22:22
  • I also found 3 occurrences of the pager value in hook_views_pre_execute() and couldn't get it work by changing any of them. – Dominic Woodman Oct 2 '14 at 22:23
1

I couldn't get hook_views_pre_build() to work as for some reason the hook wasn't being called, but did get it working by slicing the result array in hook_views_pre_render():

/**
 * Implements hook_views_pre_render()
 */
function MYTHEME_views_pre_render(&$view)
{
  if ($view->name == 'MY_VIEW' && $view->current_display == 'MY_PAGE') {
    // Keep only first item
    $view->result = array_slice($view->result, 0, 1);
  }
}

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.