1

My module prints entries from the database,
I use Pager and ->limit to choose the number of records to show per each page,
instead of hardcoding limit, I'd like user to decide, between e.g. 10,20,50,100 entries per page.

I've got form with select, and the question is - how I can save and pass user defined value to ->limit.

  1. use database - it works, but I'd like to avoid creating extra entry to database concerning such a silly setting like number of records per page
  2. variable_set - if I'll set variable e.g. number_of_rows, change in the limit made by one user will affect rest of the users - not acceptable

maybe there's some solution concerning cookies/sessions?

0

The $_SESSION variable is definitely the right place to store this data, e.g. in your form's submit handler you would do something as simple as this:

$_SESSION['MYMODULE_num_per_page'] = $form_state['values']['num_per_page'];

And when you're actually defining the form element:

$default = 10;
$current = isset($_SESSION['MYMODULE_num_per_page']) ? $_SESSION['MYMODULE_num_per_page'] : $default;

$form['num_per_page'] = array(
  '#type' => 'select',
  '#title' => 'Number per page',
  '#options' => drupal_map_assoc(array(10, 20, 50, 100)),
  '#default_value' => $current
);

// Build up the query and add $current to the ->limit() call on the query object
1

The best place maybe the drupal user object's data array for tiny amounts of information, like preferences on a per user basis.

$user->data 

I'm trying to solve this myself but these links seems useful:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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