I created a view. I added the global counter variable field. On a field below the counter field I chose to rewrite the output. In this rewritten field, if I output the counter field using {{ counter }} I get the value I would expect. That is, I get an incremental number starting at 1 for each row in the view. When I try to use Twig in the rewrite field though to test if the counter value is greater than a certain number, like {% if counter > 1 %} for example, it does not work. After several testing, it appears that the counter is always equal to zero when testing it in a Twig if statement, even though it outputs an incremental number each time.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mattferderer created an issue. See original summary.

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.

lquessenberry’s picture

I found this nasty little bugger yesterday during my attempts to use the counter in a math expression with views. You will notice that I mention this in comment #20 on this thread:

https://www.drupal.org/project/ctools/issues/2544670#comment-12342418

In conversation with @rszrama I noticed that when you dump the counter in Twig, it shows up as a 13 length string. That's odd don't you all think?

So... about that counter. LOL It's hosed

I too have noticed
This is the counter dump.
string(13) "1"

This is the NID dump.
object(Drupal\Core\Render\Markup)#1826 (1) { ["string":protected]=> string(3) "178" }

This is the title dump.
object(Drupal\Core\Render\Markup)#1811 (1) { ["string":protected]=> string(78) "<a href="/nepiswutarechicafreuishafrowaswuchobrade" hreflang="en">Distineo</a>" }

[3:55]
It thinks it's a 13 character string. :neutral_face:

gregorybesson’s picture

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.

Joe Huggans’s picture

I figured a workaround for this, I used a custom text field in the view, excluded the counter from the view in the settings and used a token to show the counter in the custom text field.

psf_’s picture

Hi,

While I writing a pad twig filter to use in views count, with rewrite, I found that the string is always "{{ counter }}", 13 chars how lquessenberry say in #4.

The rewrite that I used are:

  • {{ strpad(counter, 2, '0') }}: The funcion get "{{ counter }}" in the first parameter
  • {{ counter | pad(2, '0') }}: The filter get "{{ counter }}" in the first parameter too

I think that views is using replacement patterns in twig before it has a value, but I can't found this in code.

xdebug

Views

ruhaim’s picture

I also have bumped into this issue, but how do we allow tokens #7 to work in this case
#7joeuk31?

Joe Huggans’s picture

You create a custom text field and then use a replacement pattern to show to counter value. Since the value now comes through the custom text field in the view it works. for some reason. You understand? If you don't let me know and I will try help. :0

handkerchief’s picture

@joeuk31 not working for me :(

pixelpreview@gmail.com’s picture

the workaround of @joeuk31 not working for me too

I have created a global text field and add the "count" token in the "rewrite result" field
in an other field I use the token from the global text field not directly the token of count field and it doesn't work :(
if I do :

{% if nothing == 1 %}
...
{% endif %}

your workaround don't work, it's the same result than call directly the counter token

{% if counter == 1 %}
...
{% endif %}

Can you explain precisely you solution ?

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.

winniezz’s picture

check here https://www.drupal.org/project/drupal/issues/2764651#comment-12792086

This problem is more complicated than expected...

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.

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.

apaderno’s picture

Issue tags: -views

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

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.

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.