I'm trying to get the number of paragraphs an authenticated user inputs by counting them in the parent block template and trying to access that number in the paragraphs template. I have two templates I'm working with:
- field--field-hover-image-block.html.twig
- paragraph--hover-images.html.twig
The paragraphs are being loaded through the paragraph entity field in a custom block. I'm counting the number of the block paragraph fields with this code in that block's template, field--field-hover-image-block.html.twig:
{% for item in items %}
{% set count = items|length %}
<figure class="hover-image">{{ item.content }}</figure>
{% endfor %}
The count works, but doesn't pass to the paragraphs template.
If the count is 4 or more, I need the paragraphs template to render a class for that. Else, I don't need the class. Here is my code for the paragraphs template (paragraph--hover-images.html.twig):
{% if count >= 4 %}
{{ content.field_hover_image.0 }}
<figcaption class="four-count-plus">
<div>
<h2>{{ content.field_hover_title.0 }}</h2>
<p>{{ content.field_hover_text.0 }}</p>
</div>
</figcaption>
{% else %}
{{ content.field_hover_image.0 }}
<figcaption>
<div>
<h2>{{ content.field_hover_title.0 }}</h2>
<p>{{ content.field_hover_text.0 }}</p>
</div>
</figcaption>
{% endif %}
Any help with this would be greatly appreciated.