views_handler_field_counter.inc

  1. 3.x handlers/views_handler_field_counter.inc
  2. 2.x handlers/views_handler_field_counter.inc

Definition of views_handler_field_counter.

File

handlers/views_handler_field_counter.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_field_counter.
  5. */
  6. /**
  7. * Field handler to show a counter of the current row.
  8. *
  9. * @ingroup views_field_handlers
  10. */
  11. class views_handler_field_counter extends views_handler_field {
  12. function option_definition() {
  13. $options = parent::option_definition();
  14. $options['counter_start'] = array('default' => 1);
  15. return $options;
  16. }
  17. function options_form(&$form, &$form_state) {
  18. $form['counter_start'] = array(
  19. '#type' => 'textfield',
  20. '#title' => t('Starting value'),
  21. '#default_value' => $this->options['counter_start'],
  22. '#description' => t('Specify the number the counter should start at.'),
  23. '#size' => 2,
  24. );
  25. parent::options_form($form, $form_state);
  26. }
  27. function query() {
  28. // do nothing -- to override the parent query.
  29. }
  30. function render($values) {
  31. // Note: 1 is subtracted from the counter start value below because the
  32. // counter value is incremented by 1 at the end of this function.
  33. $count = is_numeric($this->options['counter_start']) ? $this->options['counter_start'] - 1 : 0;
  34. $pager = $this->view->query->pager;
  35. // Get the base count of the pager.
  36. if ($pager->use_pager()) {
  37. $count += ($pager->get_items_per_page() * $pager->get_current_page() + $pager->get_offset());
  38. }
  39. // Add the counter for the current site.
  40. $count += $this->view->row_index + 1;
  41. return $count;
  42. }
  43. }