You can access single value field in node template easily:
{{ content.field_test }}
or
{{ node.field_test.value }}
But iteration over multiple value fields is not pretty:
{% for key, item in content.field_tags if key|first != '#' %}
<div class="item-{{ key + 1 }}">{{ item }}</div>
{% endfor %}You can, of course, use a field template, but sometimes it doesn't make sense to have a ton of field template when you can simply have one node template.
Example:
I need to add "," between a field's values (a multiple value entity references field rendered in label). So I have a field template with:
{% for item in items %}
{{ item.content }}{% if not loop.last %},{% endif %}
{% endfor %}
And in my node template I have:
{% if content.field_facilities is not empty %}
<p>{{ content.field_facilities }}</p>
{% endif %}
But I would prefer to have a similar code directly in my node template like:
{% if content.field_facilities is not empty %}
<p>
{# This do not works! #}
{% for item in content.field_facilities %}
{{ item.content }}{% if not loop.last %},{% endif %}
{% endfor %}
</p>
{% endif %}
What would be the best way to solve that?
See also:
| Comment | File | Size | Author |
|---|---|---|---|
| #42 | 2776307-42.patch | 4.59 KB | gcalex5 |
| #37 | 2776307-37.patch | 4.17 KB | ranjith_kumar_k_u |
| #36 | 2776307-nr-bot.txt | 144 bytes | needs-review-queue-bot |
| #31 | 2776307-31.patch | 4.21 KB | jeroent |
Issue fork drupal-2776307
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
gagarine commentedComment #3
gagarine commentedComment #4
gagarine commentedComment #5
gagarine commentedThe problem is mainly with entity reference.
One solution would be to create a twig filter passing the value to Element::children
That will then be somethings like
Comment #8
mezitlab commentedRegarding Gagarin's answer, here is the code of the Twig filter that I simply put in a custom module.
mymodule/mymodule.services.yml
mymodule/src/TwigExtension/Children.php
in the Twig template (it will result a plain-text output):
Comment #10
webcultist commentedAs a front guy I can say, I already needed and still need this! Don't know how much time I already wasted on workarounds...
Comment #11
dawehnerAs a first step it would be nice to add this to https://www.drupal.org/project/twig_extensions or https://www.drupal.org/project/twig_tweak
Allowing people to have access to it as soon is possible is a nice feature.
Comment #12
anybodyYes this would be very useful in core or first in one of these helper modules. We're having that problems with paragraphs... ugly workaround.
Comment #13
pranali.addweb commentedWe can find the solution to this problem by different types of approaches:
1. I am explaining this an example. Like if we are having an array named testArray from the controller to a twig template and want to print every element.
2.
3. Another option to do it by this code
Hope this helps you.
Comment #16
jeroentThe patch attached adds a twig filter based on the code in the twig_extender module.
https://cgit.drupalcode.org/twig_extender/tree/modules/twig_extender_ext...
Comment #21
jeroentComment #22
kapilv commentedComment #23
kapilv commentedComment #24
jeroentTwig_SimpleFilter is deprecated. We should replace this with TwigFunction.
Comment #25
anmolgoyal74 commentedUpdate TwigFunction.
Comment #26
jeroentI’m sorry, i meant TwigFilter..
Also remove the leading backslash.
Comment #27
anmolgoyal74 commentedComment #30
vikashsoni commentedApplied patch #27 changes made in TwigExtension.php
Comment #31
jeroentAdded test coverage.
Comment #32
cburschkaI was about to point out the redundant is_array check but it looks like it disappeared from #31 which was added while I was typing :D
Comment #36
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #37
ranjith_kumar_k_u commentedRe-rolled #31
Comment #38
ranjith_kumar_k_u commentedComment #39
smustgrave commentedHappy to see there are tests in there.
The issue summary should be updated with what is being added, The "why" is there. Would recommend using default issue template.
If we are adding a new twig function or way to render it will need a change record.
=====
Just FYI to help get the message out there.
Starting March 2023, simple rerolls, rebases, or merges will no longer receive issue credit. Only rerolls that address a merge conflict will be credited, and the merge conflict that was resolved must be documented in the text of an issue comment.
Example
error: patch failed: core/modules/system/tests/modules/twig_theme_test/twig_theme_test.module:77
error: core/modules/system/tests/modules/twig_theme_test/twig_theme_test.module: patch does not apply
To receive credit for contributing to this issue, assist with other outstanding tasks or unaddressed feedback.
See the issue credit guidelines for more information.
Comment #41
simeWow, this gets the award for the simplest patch I didn't know i needed. Very neat.
Comment #42
gcalex5 commentedRe-roll #37 against 11.2.x