The paragraph.html.twig template file doesn't have the current loop index available to it, so we have no idea what item is currently being rendered - it is useful sometimes to know what the index is, as we might want to set some different css classes or similar on specific indexes.
To work around this, I added a field hook in my template:
function mytheme_preprocess_field(&$variables) {
if($variables['field_name'] == 'my_paragraph_field'){
foreach($variables['items'] as $idx => $item) {
$variables['items'][$idx]['content']['#paragraph']->index = $idx;
}
}
}
This enables me to access the index in the twig template using paragraph.index, but it would be preferable if this or something similar was available natively.
Comments
Comment #2
miro_dietikerI think field items also don't have it.
CSS offers some native support for children, from first/last/odd/even, ...
It could make sense to pass in the value, but i don't think we should output it by default in our template.
Comment #3
brynj commentedI'd agree that it shouldn't be output in the template by default, just made available to it.
My scenario was for a javascript carousel - I needed to set the first slide class to "active".
Comment #4
paulhuisman commentednice fix @brynj
Comment #5
bygeoffthompson commentedBravo @brynj, this node is exactly what I was looking for, and I agree, should be native.
My use case was needing to define unique classes for each iteration so I could use `paragraphs` and `bg image formatter` together easily. Thanks again!
Comment #6
alberto56 commentedAlthough @brynj's solution works perfectly for twig templates, in that {{ paragraph.index }} now outputs the index, the index does not seem to be available in the preprocessor, or at least I can't find it!
Comment #7
idebr commentedClosed #2846959: Can I pick up the delta (numerical position) within the paragraph.html.twig template as a duplicate issue.
Comment #8
osopolarClosed the issue #2658192: Delta item as duplicate. There I posted a solution for nested paragraphs (D8), which is different to the one of @brynj. I like the workaround given here, maybe the other solution is helpful for @alberto56 as it happens in the pre-processing of the child paragraph.
See also issue #3006588: Suggestion of a more flexible way to define grid layouts for a complete example of nested paragraphs used for layout purposes.
Comment #9
gappleI wanted to add the index to every paragraph type and not just a single field, so used this:
Comment #10
jankap commented@gapple could you perhaps show an example, how to reference to index in the paragraph--NAME.html.twig file?
I tried to use {{ paragraph.index }} like I can use paragraph.id() but it does not work for me.
Comment #11
gapple@JankaP
{{ paragraph.index }}is the correct way to access the property. You may need to debug your code to ensure your hook is being called and applying the value to items as expected.Comment #12
jankap commentedI only have to adapt the Theme name of the code snippet, right?
Is there a difference in working, depending how the paragraph..html.twig file is used?
I need it in a block within a view.
The custom paragraph...html.twig is working. But paragraph.index seems to be empty or not existing.
The index is for the whole paragraph field, right?
Or do I need to call if for a field inside of the paragraph?
Update:
I guess the problem is, that in a view block the variables do not have "items" but "view"
Comment #13
selwynpolit commented@brynj,
thanks for this. It worked perfectly. I put the function into my .theme file, specified the field (field_video_accordions) I used to hold my paragraph, and then I was able to output the index in a twig template as in {{ paragraph.index }} which is zero based.
Further, I was able to use a conditional to be able to add a class to the first instance of the paragraph.
Adding this in hopes it will make someone else's life a little easier.
Comment #14
mducharme commentedThis is achievable in the twig template. See the edit on the OG post here: https://drupal.stackexchange.com/questions/245027/add-odd-and-even-to-fi...
Comment #15
smulvih2#13 worked for me.
Comment #16
asarfaraz#9 worked for me for all paragraphs
Comment #17
hcanning commentedOutstanding solution guys! Works great. Will paragraph.index render in `field.html.twig`. I can just get it outputting in `paragraph.html.twig`. Thanks