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
- Create view
- 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
| Comment | File | Size | Author |
|---|---|---|---|
| #33 | 2361507-after_patch-29.png | 196.09 KB | abhijith s |
| #32 | Screenshot 2022-08-24 at 15.05.29.png | 91.92 KB | suparnaa.dey |
| #29 | interdiff_21-29.txt | 496 bytes | ranjith_kumar_k_u |
| #29 | 2361507-29.patch | 2.26 KB | ranjith_kumar_k_u |
| #21 | 2361507-global-view-reverse-counter-d8_2.patch | 2.25 KB | ohorbatiuk |
Issue fork drupal-2361507
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
Comment #1
polHere's the patch for 7.x-3.x
Comment #2
dawehnerJust a really quick feedback. ... Would you also have time to forward port it later to Drupal 8?
Note: reverse is a boolean, so let's try to treatt it as such. Therefore use 'bool' => TRUE in option_definition()
Comment #3
polHere's the patch for D8 with the previous remarks fixed.
Comment #4
polPatch for D7.
Comment #5
polHere's the patch for d8 and the tests.
Comment #6
polUpdated patch for Drupal 7, works now with pager too.
Comment #7
polUpdated patch for Drupal 8, works with pager too.
Comment #9
polThanks :)
Do I need to do something for Drupal 8 ?
Comment #10
loopduplicate commentedVery cool! Thanks Pol :)
Comment #11
polYou're welcome !
Comment #12
dawehnerSomeone needs to work on the patch itself.
Comment #15
catchComment #16
polWill work on this very soon.
Comment #21
ohorbatiukComment #23
Prabu_Ela commentedhi, 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.
Comment #25
toamit commentedThis 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.
Comment #28
bhanojeerao commentedThis is useful feature, better to have this option on Core. I used it in a Drupal 8 portal. Thanks.
Comment #29
ranjith_kumar_k_u commentedFixed CS error.
Comment #31
manishsaharan commentedThis is actual a useful feature, Better to have this in core itself.
Comment #32
suparnaa.dey commentedI have tested #23 on Drupal 9.4.x with php 8.0 and its works as expected.
Comment #33
abhijith s commentedApplied 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 pagerI was having 5 contents and its only starting from 3.
Comment #34
abhijith s commentedComment #37
johnvComment #38
rschletty commentedThe 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!
Comment #39
nicxvan commentedThis needs an issue summary update.
@rschletty you might consider updating to a newer version of drupal. Drupal 6 has not received updates since 2017.
Comment #40
abhijith s commentedComment #42
abhijith s commentedUpdated patch #29 and created MR. Fixed the issue encountered with mini-pager in #33
Comment #43
abhijith s commentedComment #44
abhijith s commentedComment #45
lendudeWe 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.
Comment #46
abhijith s commentedHi @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.
Comment #48
stolzenhain commentedPatch works great! when will this get a "reviewed & tested" tag?