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.

Problem/Motivation

We need to implement a reverse counter for the view results. While there is currently a counter in place, it counts in ascending order, which doesn't meet our requirements. Instead, we need a descending counter that starts from the total and counts down.

Steps to reproduce

  1. Create view
  2. Add View result counter field and enable Reverse option

Proposed resolution

Add option to reverse count in already existing view result counter field /core/modules/views/src/Plugin/views/field/Counter.php

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-2361507

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

pol’s picture

StatusFileSize
new1.99 KB

Here's the patch for 7.x-3.x

dawehner’s picture

Just a really quick feedback. ... Would you also have time to forward port it later to Drupal 8?


+++ b/handlers/views_handler_field_counter.inc
@@ -14,6 +14,7 @@ class views_handler_field_counter extends views_handler_field {
+    $options['reverse'] = array('default' => 0);

@@ -34,16 +42,21 @@ class views_handler_field_counter extends views_handler_field {
+    $reverse = ($this->options['reverse'] == 0) ? 1 : -1;

Note: reverse is a boolean, so let's try to treatt it as such. Therefore use 'bool' => TRUE in option_definition()

pol’s picture

Here's the patch for D8 with the previous remarks fixed.

pol’s picture

Patch for D7.

pol’s picture

Here's the patch for d8 and the tests.

pol’s picture

Updated patch for Drupal 7, works now with pager too.

pol’s picture

Updated patch for Drupal 8, works with pager too.

  • dawehner committed 82634af on 7.x-3.x authored by Pol
    Issue #2361507 by Pol: Added Global: View result reverse counter .
    
pol’s picture

Thanks :)

Do I need to do something for Drupal 8 ?

loopduplicate’s picture

Very cool! Thanks Pol :)

pol’s picture

You're welcome !

dawehner’s picture

Project: Views (for Drupal 7) » Drupal core
Version: 7.x-3.x-dev » 8.0.x-dev
Component: Views Data » views.module
Status: Needs review » Patch (to be ported)

Someone needs to work on the patch itself.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

catch’s picture

Status: Patch (to be ported) » Needs work
pol’s picture

Will work on this very soon.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

ohorbatiuk’s picture

Status: Needs work » Needs review
StatusFileSize
new2.25 KB

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Prabu_Ela’s picture

hi, I have applied this patch (PHP 7.1 & MySQL 5.7 24,545 pass) the reverse order is working fine but it removes off all the view exposed filter with dropdown.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

toamit’s picture

This is a useful feature, that would be nice to incorporate in the core. I have run into a need for this on a set of sites.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
bhanojeerao’s picture

This is useful feature, better to have this option on Core. I used it in a Drupal 8 portal. Thanks.

ranjith_kumar_k_u’s picture

StatusFileSize
new2.26 KB
new496 bytes

Fixed CS error.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

manishsaharan’s picture

This is actual a useful feature, Better to have this in core itself.

suparnaa.dey’s picture

StatusFileSize
new91.92 KB

I have tested #23 on Drupal 9.4.x with php 8.0 and its works as expected.

abhijith s’s picture

StatusFileSize
new196.09 KB

Applied patch #29 on 9.4.x.The reverse counter is not working correctly for me when I select the pager type in view as mini pager

I was having 5 contents and its only starting from 3.

abhijith s’s picture

Status: Needs review » Needs work

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

johnv’s picture

Title: Global: View result reverse counter » Add 'reverse' option to 'Global: View result counter' field
rschletty’s picture

The code in the original post works fine for me in Drupal 6. I replaced the code in views_handler_field_counter.inc and now I have the Reverse option which works for my list of over 1200 members: https://www.topcatholicsongs.com/users

I have views-6.x-3.11 installed on my site.

Thank you, pol!

nicxvan’s picture

This needs an issue summary update.

@rschletty you might consider updating to a newer version of drupal. Drupal 6 has not received updates since 2017.

abhijith s’s picture

abhijith s’s picture

Updated patch #29 and created MR. Fixed the issue encountered with mini-pager in #33

abhijith s’s picture

Status: Needs work » Needs review
abhijith s’s picture

Issue summary: View changes
lendude’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests, +Needs upgrade path

We shouldn't change the behaviour for the mini pager here, it is out of scope. The mini pager doesn't do a count query and it should stay that way. And if we would want to change that, it should be its own issue.

This needs tests, and upgrade path for the new setting and a test for the upgrade.

abhijith s’s picture

Hi @lendude,

The behaviour of the mini-pager remains unchanged in the latest merge request. However, the previous patch #29 did not work correctly with the Views mini-pager configuration, as I previously mentioned in comment #33. This issue has been addressed and resolved in the latest merge request.

Just to highlight, the Global: View result counter is already functioning with full and mini-pagers, as well as other pagination methods—so comprehensive support is expected with reverse option as well.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

stolzenhain’s picture

Patch works great! when will this get a "reviewed & tested" tag?