Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

Say for instance, you have a view that displays when viewing a certain node type, but you want the pager settings to be different for each individual node. I was thinking of making a CCK field like "pager setting" and then give it an integer option of x-y. But I don't know if it's possible to somehow dynamically plug that field into the Views' settings. Or is there another way to do this maybe?

share|improve this question

4 Answers

up vote 11 down vote accepted

The views hook that you would want to use is hook_views_pre_build which is called before the query is build. 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 thats call before the view query is build that way the pager and everything else will reflect the change.

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

Hope this helps.

share|improve this answer
Awesome, thank you. I don't have any module development experience yet, but I'm starting out on it for a couple of things I want to accomplish. I do have a working grasp of PHP, so I can follow along what's happening in that code chunk you pasted above. I think this well help me a lot. Thank you. – Jay Mar 30 '11 at 6:46

You should to use views preprocess function

/*
 * Implementation of hook_views_pre_render().
 */
function MYMODULE_views_pre_render(&$view){
  // $view->name
  // $view->current_display
  // ...
  // look for other variables in $view object
}
share|improve this answer
"preprocess" is for theming and pre_render is too late (he query was allready run)- the pre_build hook is much better. – mojzis Jan 6 at 11:32

To update views result and pager in hook_views_pre_render, you can do following:

<?php

/**
 * Implementation of hook_views_pre_render().
 */
function MODULENAME_views_pre_render(&$view) {
  if ($view->name == 'my_view' && $view->current_display == 'my_display') {
    // View result update logic.
    // e.g.
    // $result = array();
    // foreach ($view->result as $k => $row) {
    //   if (whatever is your condition) {
    //     $result[$k] = $row;
    //   }
    // }

    // Assuming $result has data as per your logic.
    // Update the pager according to result.
    $view->query->pager->total_items = count($result);
    $view->query->pager->update_page_info();
    // Add results to view.
    $view->result = $result;
  }
}

This should work!! ;)

share|improve this answer
Excellent! I wanted to limit the number of items on a given page depended on the items returned by the query. Will check out this method ASAP. – Jens May 19 at 13:22
It does not exactly work. The rows you discharge does not turn up on the next page. So you end up with missing content. Needs more investigation. – Jens May 19 at 14:44

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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