Drupal - Is it possible to dynamically set Views' pager settings?

The views hook that you would want to use is hook_views_pre_build which is called before the query is built. Now this is assuming you have some basic module development experience and that you are familiar with the views api.

You should be able to do :

/*
 * Implementation of hook_views_pre_build().
 */
function hook_views_pre_build(&$view) {

  // Make sure this is only for the specific view you want to modified
  if ($view->name == "foo_bar") {

    // Get the x-y value from where you're storing it (in your example the node object).
    $pager_count = get_count_for_this_node();

    // Lets also make sure that this is a number so we won't destroy our view.
    if (is_numeric($pager_count)) {

      // Now lets set the pager item to what ever out count is.
      $view->pager['items_per_page'] = $pager_count;
    }
  }
}

Above we're using a views hook that's called before the view query is built that way the pager and everything else will reflect the change.

Word of caution: views hooks should only be used if you understand whats going on. The above code is written for views-2.x.

Hope this helps.


For Drupal 7, Only should write the following:

$view->items_per_page = $pager_count;

In the example:

/**
 * Implements hook_views_pre_build().
 */
function module_name_views_pre_build(&$view) {
  if ($view->name == "foo_bar" && $view->current_display == 'foo_display') {
    $pager_count = get_count_for_this_node();
    if (is_numeric($pager_count)) {
      $view->items_per_page = $pager_count;
    }
  }
}

I use code example by @ericduran.

Tags:

Views

7