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.
Comment | File | Size | Author |
---|---|---|---|
#8 | Captura de pantalla de 2018-05-22 09-35-17.png | 20.99 KB | psf_ |
#8 | Captura de pantalla de 2018-05-22 09-08-46.png | 150.55 KB | psf_ |
Comments
Comment #4
lquessenberry CreditAttribution: lquessenberry for E.C. Barton & Co commentedI 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:
Comment #5
gregorybesson CreditAttribution: gregorybesson commentedSee a possible solution there: https://www.drupal.org/project/drupal/issues/2764651#comment-12358994
Comment #7
Joe HuggansI 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.
Comment #8
psf_ CreditAttribution: psf_ at SDOS commentedHi,
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:
I think that views is using replacement patterns in twig before it has a value, but I can't found this in code.
Comment #9
ruhaim CreditAttribution: ruhaim commentedI also have bumped into this issue, but how do we allow tokens #7 to work in this case
#7joeuk31?
Comment #10
Joe HuggansYou 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
Comment #11
handkerchief@joeuk31 not working for me :(
Comment #12
pixelpreview@gmail.com CreditAttribution: pixelpreview@gmail.com commentedthe 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 ?
Comment #14
winniezz CreditAttribution: winniezz commentedcheck here https://www.drupal.org/project/drupal/issues/2764651#comment-12792086
This problem is more complicated than expected...
Comment #17
apaderno