Reputation: 43
This is my setup. I have a custom block type that has a reference entity field. It references a content type of "Products". The Products content type has a reference entity field to a taxonomy vocabulary of "Series".
The Series taxonomy contains a field that I need to get the value of in my custom block's product field theme. I basically have a block of 5 products, each product belonging to a series. In my theme, I need to apply a series field value to each product.
Is there a way to get to this value strictly from twig? I have tried countless combination chains to try to get to it.
I have my block--bundle--product_series_block.html.twig file that surrounds the products.
<div {{ attributes.addClass(classes) }}
data-ng-controller="ProductsController as vm">
<div class="container">
{{ title_prefix }}
{% if label %}
<h2{{ title_attributes }}>{{ label }}</h2>
{% endif %}
{{ title_suffix }}
<div class="product-holder">
{% block content %}
{{ content }}
{% endblock %}
</div>
</div>
</div>
Then that goes to my field--field-products.html.twig where I want to get the series for use in a data-series html attribute.
<div{{ attributes.addClass(classes, 'field__items') }} data-series="{{ cant_figure_this_out }}">
{% for item in items %}
<div{{ item.attributes.addClass('field__item item') }}>{{ item.content }}</div>
{% endfor %}
</div>
Upvotes: 4
Views: 7948
Reputation: 624
1/ You can get it at node template level with something like this:
node.field_serie.0.get('entity').getTarget().getValue().getName()
If you want it as an array ...:
{% set series = [] %}
{% for key, item in node.field_serie %}
{% set series = series|merge( [item.get('entity').getTarget().getValue().getName()] ) %}
{% endfor %}
2/ You can also get it at field template level:
{% set series = [] %}
{% for key, item in item['content']['#node'].field_serie %}
{% set series = series|merge( [item.get('entity').getTarget().getValue().getName()] ) %}
{% endfor %}
3/ Then you can use something like (this probably needs more work):
attributes.setAttribute('data-series', series|join(',')|escape
Upvotes: 3