Is Letting your user change number of rows returned $limit possible for views 6.x-2.11?

I am able to complete the first five (5) steps and have the exposed filter with the choices available at the top of the view. I am using the following argument code:

if( !$_GET['filter0'] ) {
  $_GET['filter0'] = $limit;
}
else if (is_numeric($_GET['filter0'])) {
  $limit = $_GET['filter0'];
}
else {
  $limit = 100;
}
unset($filters[0]);

I am also using the Basic Validator to display all values if the argument does not validate.

If I make any selection, the view returns empty text. If I select -Any-, the view returns all values.

I am not sure where to start troubleshooting this or if there is a better approach.

CommentFileSizeAuthor
#9 set_items.txt4.38 KBwaverate

Comments

merlinofchaos’s picture

Status: Active » Fixed

I think in your argument code you can use $view->set_items_per_page($limit); In Views 3 you can do this from the UI.

waverate’s picture

Status: Fixed » Active

I just did a quick non-production install of Views3 and it looks like this feature is nicely implemented.

However, I don't know if all my modules are ready to work with Views3, so I think I need to hold off on that.

That being said, I think my problem lies in the argument code itself in addition to adding $view->set_items_per_page($limit);

Is unset($filters[0]); still valid in Views2?

dawehner’s picture

Title: D6: Letting your user change number of rows returned $limit » Changed items_per_page based on argument input

unset($filters[0][); is could for views 1.x, so it will not work at all.

waverate’s picture

Thank you. I was beginning to suspect that. Is there an easier way to change the $limit for views2.x than the original argument code?

dawehner’s picture

Status: Active » Fixed

Well you have to write the code.
Earl already described a single function which has to be coded somewhere, in the argument validator/in a hook.

dawehner’s picture


$view = new view;
$view->name = 'blub';
$view->description = '';
$view->tag = '';
$view->base_table = 'node';
$view->api_version = '3.0-alpha1';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Defaults */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->display->display_options['access']['type'] = 'none';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'fields';
/* Field: Node: Nid */
$handler->display->display_options['fields']['nid']['id'] = 'nid';
$handler->display->display_options['fields']['nid']['table'] = 'node';
$handler->display->display_options['fields']['nid']['field'] = 'nid';
$handler->display->display_options['fields']['nid']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['nid']['alter']['make_link'] = 0;
$handler->display->display_options['fields']['nid']['alter']['absolute'] = 0;
$handler->display->display_options['fields']['nid']['alter']['trim'] = 0;
$handler->display->display_options['fields']['nid']['alter']['word_boundary'] = 1;
$handler->display->display_options['fields']['nid']['alter']['ellipsis'] = 1;
$handler->display->display_options['fields']['nid']['alter']['strip_tags'] = 0;
$handler->display->display_options['fields']['nid']['alter']['html'] = 0;
$handler->display->display_options['fields']['nid']['element_label_colon'] = 1;
$handler->display->display_options['fields']['nid']['element_default_classes'] = 1;
$handler->display->display_options['fields']['nid']['hide_empty'] = 0;
$handler->display->display_options['fields']['nid']['empty_zero'] = 0;
$handler->display->display_options['fields']['nid']['link_to_node'] = 1;
/* Argument: Global: Null */
$handler->display->display_options['arguments']['null']['id'] = 'null';
$handler->display->display_options['arguments']['null']['table'] = 'views';
$handler->display->display_options['arguments']['null']['field'] = 'null';
$handler->display->display_options['arguments']['null']['default_action'] = 'default';
$handler->display->display_options['arguments']['null']['style_plugin'] = 'default_summary';
$handler->display->display_options['arguments']['null']['default_argument_type'] = 'fixed';
$handler->display->display_options['arguments']['null']['default_argument_options']['argument'] = '0';
$handler->display->display_options['arguments']['null']['validate_type'] = 'php';
$handler->display->display_options['arguments']['null']['validate_options']['code'] = '$view->set_items_per_page(rand(0, 10));
return TRUE;';
$handler->display->display_options['arguments']['null']['must_not_be'] = 0;
$translatables['blub'] = array(
  t('Defaults'),
  t('more'),
  t('Apply'),
  t('Reset'),
  t('Sort By'),
  t('Asc'),
  t('Desc'),
  t('Items per page'),
  t('- All -'),
  t('Offset'),
  t('Nid'),
  t('All'),
);

This view shows an example how to set the items per page based on random.
You should be able to do the rest.

waverate’s picture

Great help. Now I understand what how to write argument code. The only subtlety is that the export in #7 is for views3.x. I have attached an export of the above example for views 2.x.

In the end I dropped the Exposed Filter from the original post in favour of this header:

<div style="text-align: right;">Rows: <a href="/test/5">5</a> <a href="/test/10">10</a> <a href="/test/25">25</a> <a href="/test">All</a></div>

From #7, I added a Global: Null argument with the following:

- display all values if arguement is not present
- validator: PHP code
- display all values if the argument does not validate
- PHP validate code:

if (is_numeric($argument)) {
$view->set_items_per_page($argument);
return TRUE;
}

I did notice that in order to get the pager working correctly, the items per page had to be greater than the highest argument that you intend to pass.

Where I am running into problems now is with the links in the header and the links generated by my sortable table. For example, if I sort my table in like:

http://example.com/test?order=nid&sort=desc

and I want to display 10 results per page, I cannot use the link in my header (http://example.com/test/10). I need to somehow generate http://example.com/test/10?order=nid&sort=desc.

Any ideas?

waverate’s picture

StatusFileSize
new4.38 KB

Attachment for #7

meishme’s picture

Perfect timing. This solution is perfect for what I need.

waverate’s picture

It is a good solution if you don't have sortable columns or exposed filters. If that is the case, I would change the header to PHP and try and make it work with the other URL arguments.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.