I have a field on a node type that references another node type. On the display side, I am setting it to a view mode to be rendered.
What I want to do is add a zebra stripe class. Every other node needs an extra class on its wrapper. I figure this should be done at the field twig level, but how do you pass in a value when it is rendered?
field--field-related-content.html.twig
:
{% for item in items %}
{{ item.content }}
{% endfor %}
In the node view mode template:
<a class="hl__type-promo" href="{{ path('entity.node.canonical', {'node': node.id() }) }}" title="{{ node.getTitle() }}" style="background-image: url('../../assets/images/fpo_16x9.png')">
<div class="hl__type-promo__header">
<div class="hl__type-promo__icon">
{% include "@mytheme/svg/general/how-to.html.twig" %}
</div>
<div class="hl__type-promo__subject">How-To</div>
</div>
<div class="hl__type-promo__details"><h3 class="hl__type-promo__title">{{ label }}</h3>
<div class="hl__type-promo__description">
DESCRIPTION HERE
</div>
</div>
</a>
The node is unaware of where it is, so, whats the best way to pass a class? Is that possible? The field.html.twig file in classy wraps the field with a div, but thats not what I need to do here.
I suppose one way to do it is change the field twig to something like:
{% for item in items %}
{% set classes = '...' %}
<a class="{{ classes }}" href="{{ path('entity.node.canonical', {'node': item.content['#node'].id() }) }}" title="{{ item.content['#node'].getTitle() }}" style="background-image: url('../../assets/images/fpo_16x9.png')">
{{ item.content }}
</a>
{% endfor %}
But that feels dirty, and doesn't really make it reusable, not to mention I would have to inject the background image url through a field preprocess instead of just handle it in the node rendering.
:nth-child(even/odd)
or a bootstrap class.