Using a settings page to change the number of results in a view

Many sites have views that are used to generate a large number of sections/pages on the site, and you may want to easily change the number of results without having to edit the view. It’s easy to create a settings page that will allow you to configure the number of results.

Let’s assume that we already have a view created (“articles”) that contains a display (“page_1”) that displays all the articles on the site.

Brad Blake
#Drupal | Posted

Many sites have views that are used to generate a large number of sections/pages on the site, and you may want to easily change the number of results without having to edit the view. It’s easy to create a settings page that will allow you to configure the number of results.

Let’s assume that we already have a view created (“articles”) that contains a display (“page_1”) that displays all the articles on the site. It takes an argument of a taxonomy term that if present, filters the articles to those tagged with that term.

First, we’ll create a settings page where someone can change the number of results, and adjust the permissions of who can change the settings.

  1. function modulename_menu() {
  2. $links = array();
  3.  
  4. $links['admin/settings/results-changer'] = array(
  5. 'title' => t('Adjust the number of results'),
  6. 'description' => t('Adjust the number of results per section or page.'),
  7. 'page callback' => 'drupal_get_form',
  8. 'page arguments' => array('modulename_change_results'),
  9. 'access arguments' => array('access change results'),
  10. );
  11.  
  12. return $links;
  13. }

On the settings page, we could create a field that would allow us to globally change the number of results for a particular view.

  1. function modulename_change_results() {
  2. $form = array();
  3.  
  4. $form['modulename_results_page_x'] = array(
  5. '#type' => 'textfield',
  6. '#title' => 'The number of results to display on Page X',
  7. '#size' => '3',
  8. '#default_value' => variable_get('modulename_results_page_x', 10),
  9. );
  10.  
  11. return system_settings_form($form);
  12. }

But what if this view is responsible for outputting a number of different pages, each with a different taxonomy term? In this case, we can loop through the terms to create a different variable for each.

  1. $terms = taxonomy_get_tree($tid);
  2. foreach ($terms as $term) {
  3. $form['modulename_' . $term->tid . '_results'] = array(
  4. '#type' => 'textfield',
  5. '#title' => 'Number of Articles to show on ' . $term->name . ' page',
  6. '#size' => '3',
  7. '#default_value' => variable_get('modulename_' . $term->tid . '_results', 10),
  8. );
  9. }

Now that we’ve stored the number of results we want to display on our pages, we need to alter our view to make use of these settings. In this example, we’re simply using the global setting.

  1. function modulename_views_query_alter(&$view, &$query) {
  2. if ($view->name = = "articles" && $view->current_display == "page_1") {
  3. $count = variable_get('modulename_results_page_x', 10);
  4. $view->set_items_per_page($count);
  5. }
  6. }

If we want to use our variables that are based on taxonomy terms, we could do this instead:

  1. $tid = $query->where['0']['args'][1];
  2. // Use the variable if set, otherwise use the default for the view, otherwise default to 10
  3. if (!empty($tid)) {
  4. $count = variable_get('modulename_' . $tid . '_results', $view->display_handler->get_option('items_per_page', 10));
  5. $view->set_items_per_page($count);
  6. }

With that, you’ve changed the number of results that a view generates without having to modify the view.

Brad Blake