Tuesday, November 3, 2015

Drupal custom pagination - override theme_pager

You can override Drupal pagination using below code.

I have faced below issue in one of my project.

ie, Actual total query result is different than displaying result.

Ex: My query total count was 100. But while displaying, based on requirement we need to display only 80 items. So when I use theme('pager') in template, I got result for 100 which is wrong. We need to display pager for only 80 items.

I have done below changes to override Drupal pagination.
// Menu callback function
function display_some_items() {
  // Query to fetch data with pager.
  $data['result'] = It contains filtered items, not equal with actual total count.
  $data['custom_pager'] = my_custom_pager($filtered_count); // custom pagination callback.
  $output .= theme('some_tpl_file', $data);
}

// custom pagination
function my_custom_pager($filtered_count = 0) {
  pager_default_initialize($filtered_counts, 10); // Pagination Limit 10
  $args = array('quantity' => 10,'tags' => array('<<','<','','>','>>'));
  return theme('pager', $args);
}

// Print below code in your template
print $custom_pager; // It will display pager based on filtered count.


No comments:

Post a Comment