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

Posted Jul 29, 2010 // 1 comments
Brad :

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

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

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

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

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

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

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

About Brad

Brad Blake is a Web Developer with expertise in developing software tools and web sites on the LAMP platform. He has been developing cutting-edge technologies using PHP for the past 5 years.

Prior to joining Phase2, Brad was a ...

more >

Read Brad 's Blog

Comments

by Chris Cohen (not verified) on Thu, 07/29/2010 - 11:30

What about user-controlled?

Thanks for the write-up. It's a very effective way of altering a view 'on-the-fly'. What about if we wanted to give the anonymous user control over how many results are shown on the page?

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Allowed HTML tags: <a> <strong> <code> <p> <img> <ul> <ol> <li> <h2> <h3> <h4> <b> <u> <i>
  • You may insert videos with [video:URL]

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.