Fix for global view counter

Created on 22 August 2017, over 7 years ago
Updated 3 September 2024, 4 months ago

The global view result counter passes itself wrapped in double curly braces when rendered, rather than as an integer value. This patch fixes that by adding a render function in Drupal\views\Plugin\views\field\Counter.php.

So, for example, a drupalist can put a global view counter and a global custom text field in a view, and then to add filters to counter in the text replacement field.

Fields:
Global: View result counter
Global: Custom text

In the custom text field:
{% if counter is even %}
... do something ...
{% endif %}

This was before not possible, as the string "{{ counter }}" was passed to the "even" filter, rather than the value of counter itself.

๐Ÿ“Œ Task
Status

Needs work

Version

11.0 ๐Ÿ”ฅ

Component
Viewsย  โ†’

Last updated about 5 hours ago

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States lincolnbergeson

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

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

  • ๐Ÿ‡ญ๐Ÿ‡บHungary djg_tram

    It's still broken in Drupal 10.

  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands Summit

    Hi,
    And needed for Drupal 6/7 to Drupal 10 Migration forward, and already broken in D7, D8 I think. Sorry not wanting to hiijack this issue, but clarifiing, and may be a solution is than possible for Drupal 10 back to Drupal 7: https://www.drupal.org/project/vbo_export/issues/3359435 ๐Ÿ› View Result Counter is broken on exports that use batching Active
    if of course it is the same issue. Otherwise, sorry to disturb this issue.
    greetings,

  • last update about 1 year ago
    28,522 pass, 3 fail
  • The same problem would not be feasible to be able to make a custom module that does that using patch 14 and 15 because it has been so long since there is still no news about this

  • ๐Ÿ‡ต๐Ÿ‡ฑPoland sebaz

    I've made my own counter. It is just a plugin to the Views module so copy the original one, inherit the original class and override methods shown in patches. Works a hell :)

  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands Summit

    Hi SebaZ, willing to share your own counter? Thanks for your reply in advance,
    Greetings,

  • ๐Ÿ‡ต๐Ÿ‡ฑPoland sebaz

    Counter is written per project so I can't share it.

    Just extend Counter class.
    Remember that unique name of counter which is ViewField you have to set in annotation. You will need config schema also.

  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands Summit

    Hi Sebaz,

    Thanks for the info. I am not a programmer, so are not able to build it.
    But maybe it is great for someone to use it to build a add-on for views, or to add it to views.
    Thanks!
    greetings,

  • ๐Ÿ‡ต๐Ÿ‡ฑPoland sebaz

    Try this module.
    As I mentioned before. It is a copy of Counter field in Views called Counter fixed output. This module extends core Counter Views plugin class and overrides

    render()
    

    method.

  • ๐Ÿ‡ง๐Ÿ‡ชBelgium mgstables

    Thanks SebaZ it works perfectly.
    For a taxonomy view, used as a filter menu, I needed an extra class โ€œactiveโ€ for the first row.
    I tried with the views counter and that didn't work.
    Now with this "Counter fixed output" it works.
    {% if counter_fixed == '1' %}active {% endif %}

    Drupal 10.2.1
    PHP 8.2

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States tory-w

    The module in #37 works for me. Use case was building a view where I want every result that ends with "5", for example to have an attachment; say like an ad. I used this field to number each result, added a conditional field with the in-feed ad, and it works! It did not work with the core Global: View result counter.

    Drupal 10.3.2
    PHP 8.3.10

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia prashant.c Dharamshala

    prashant.c โ†’ made their first commit to this issueโ€™s fork.

  • Merge request !9397Draft: Resolve #2903924 "Fix for global" โ†’ (Open) created by prashant.c
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia prashant.c Dharamshala

    Is this issue still valid? Because now the value is being returned in the following way:

     public function getValue(ResultRow $values, $field = NULL) {
        // Note:  1 is subtracted from the counter start value below because the
        // counter value is incremented by 1 at the end of this function.
        $count = is_numeric($this->options['counter_start']) ? $this->options['counter_start'] - 1 : 0;
        $pager = $this->view->pager;
        // Get the base count of the pager.
        if ($pager->usePager()) {
          $count += ($pager->getItemsPerPage() * $pager->getCurrentPage() + $pager->getOffset());
        }
        // Add the counter for the current site.
        $count += $this->view->row_index + 1;
    
        return $count;
      }
    
  • ๐Ÿ‡ต๐Ÿ‡ฑPoland sebaz

    It never was issue about getting the value but how it is rendered.

    Method getValue() vs render()

Production build 0.71.5 2024