1

I would like to create a custom form to allow a website admin to quickly change the number of "Items to Display" for a particular block display for a particular view, but I don't have a clue how to change this value programmatically on my own, besides the views ui?

0

First, you retrieve the view with views_get_view(), then you alter the value, then you save the retrieved view with views_save_view() like this:

function my_custom_function($view, $num) {
  $myview = views_get_view('$view', TRUE);

  $myview->display['default']->display_options['pager']['options']['items_per_page'] = $num;
  views_save_view($myview);

  // Clear the cache for the pager
  $cache = $myview . ':block:results:';
  cache_clear_all($cache, 'cache_views_data', TRUE);
}

I also included a little snippet at the end of the custom function to clear the appropriate cache bin if need be.

0

im not sure of any 3rd party module that allows easy "override the # of items per page". I've done this task a few times for View displays using core drupal apis.

Basically:

  1. you use hook_views_pre_build to programmatically alter the view.
  2. Use a custom variable to store your items per page value.
  3. Make a simple admin page to allow a site administrator alter the value.

EDIT: amended to different views hook function.

  • tenken, how would I "use hook_views_pre_render to programmatically alter the view"? I tried: function glue_views_pre_render(&$view) { if ($view->name == 'news') { $view->query->pager->options['items_per_page'] = '2'; } } to no avail. – carsonw May 6 '13 at 22:57
  • im catching a ride soon. try the same thing in hook_views_pre_build() ... what you have looks correct. api.drupal.org/api/views/views.api.php/function/… – tenken May 6 '13 at 23:03
  • I think you would be better using hook_views_pre_build() and then somthing like that $view->items_per_page = variable_get('custom_view_items', 10); – tostinni Jun 3 '13 at 2:59

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.