On a content type called case_study, I have a taxonomy term entity reference field called field_case_study_technologies.
I'm trying to theme it to add a link to a view, rather than to the taxonomy term itself.
When trying to use twig filters on the field item content, the output is blank, and the following notice is logged:
Warning: mb_strtolower() expects parameter 1 to be string, array given in twig_lower_filter() (line 1293 of /var/www/sp/vendor/twig/twig/lib/Twig/Extension/Core.php).
Here is the relevant extract from the field template.
{% for item in items %}
<a href="/case-studies/{{ item.content | lower }}">
{{ item.content }}
</a>
{% endfor %}
I guess it may be necessary to use template_preprocess_field to achieve this?
Comments
Comment #2
malcomio commentedI've achieved the desired result in the preprocess, but it feels like it should be possible to do it with Twig.
Not sure what the "correct" way of doing it would be...
For reference, here's what I did:
Comment #3
star-szrIf you really want to do it in Twig you might have to render the render array into a variable or just use the |render filter.
Glad you got it figured out.
IMO this really sounds like a job for a field formatter, not preprocess nor Twig. It's really not a theming thing. Because something is possible doesn't mean it's the way to go!
Comment #5
AniaMi commented#3 - how to do it in twig?
I need to print field (field_image) which is field references to media_image, which can have multiple values (images), but I want to print only the first one. How to do it in twig in node.html.twig file?