Add 'reverse' option to 'Global: View result counter' field

Created on 22 October 2014, over 10 years ago
Updated 26 January 2025, 1 day ago

Hi all,

We need to add a reverse counter to our views result.
Currently the View result counter exists but it's not really what I need, instead of having an ascending counter, I need a reverse counter that count descending.

So I made some changes in the views_handler_field_counter.inc, here they are.

/**
 * @file
 * Definition of views_handler_field_counter.
 */

/**
 * Field handler to show a counter of the current row.
 *
 * @ingroup views_field_handlers
 */
class views_handler_field_counter extends views_handler_field {
  function option_definition() {
    $options = parent::option_definition();
    $options['counter_start'] = array('default' => 1);
    $options['reverse'] = array('default' => 0);
    return $options;
  }

  function options_form(&$form, &$form_state) {
    $form['counter_start'] = array(
      '#type' => 'textfield',
      '#title' => t('Starting value'),
      '#default_value' => $this->options['counter_start'],
      '#description' => t('Specify the number the counter should start at.'),
      '#size' => 2,
    );

    $form['reverse'] = array(
      '#type' => 'checkbox',
      '#title' => t('Reverse'),
      '#default_value' => $this->options['reverse'],
      '#description' => t('Reverse the counter.'),
    );

    parent::options_form($form, $form_state);
  }

  function query() {
    // do nothing -- to override the parent query.
  }

  function render($values) {
    $reverse = ($this->options['reverse'] == 0) ? 1 : -1;

    // Note:  1 is subtracted from the counter start value below because the
    // counter value is incremented by 1 at the end of this function.
    $counter_start = is_numeric($this->options['counter_start']) ? $this->options['counter_start'] : 0;
    $count = ($reverse == -1) ? count($this->view->result) + $counter_start : $counter_start -1;
    $pager = $this->view->query->pager;

    // Get the base count of the pager.
    if ($pager->use_pager()) {
      $count += (($pager->get_items_per_page() * $pager->get_current_page() + $pager->get_offset())) * $reverse;
    }

    // Add the counter for the current site.
    $count += ($this->view->row_index + 1) * $reverse;

    return $count;
  }
}

If it's useful, I can make a patch and to have it inside Views by default.

Feature request
Status

Needs work

Version

11.0 🔥

Component

views.module

Created by

🇧🇪Belgium pol Brussels

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024