Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Using the computed twig element, I would like to know how to output the name of a referenced entity as selected in a radio or checkbox element on the prior form page.
- Using the Computed token element, I am successful with this:
[webform_submission:values:option:entity:title]
. - Using the Computed twig element, I am successful outputting the referenced entity id with this:
{{ data.option }}
. - I can also output a Text field element using this:
{{ data.first_name }}
- However, using the Computed twig element, I am not successful using this:
{{ data.option.entity.title }}
.
Comment | File | Size | Author |
---|---|---|---|
#16 | Screen Shot 2017-11-29 at 10.20.00 PM.png | 81.58 KB | jrockowitz |
#3 | 2926806-twig-help-3.patch | 1.53 KB | jrockowitz |
| |||
#3 | Screen Shot 2017-11-28 at 10.06.09 AM.png | 106.02 KB | jrockowitz |
Comments
Comment #2
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedTry using
{{ webform_token('[webform_submission:values:option:entity:title]', webform_submission) }}
.We need to better document the
webform_token()
Twig function in the UI.Comment #3
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commented..and the inline Twig help was broken.
The attached patch fixes the issue.
Comment #4
Adrian83 CreditAttribution: Adrian83 commented@jrockowitz Thank you, the twig token worked for calling a single value. I also applied the patch which fixed the twig help. That is really helpful. When using the twig token, is it possible to do the twig for loop when outputting titles for a multi-value reference field? Or maybe I would be better off working with this: {{ data.element_key.delta }}?
Comment #6
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedI committed the patch. Please download the latest dev release to review.
Comment #7
Adrian83 CreditAttribution: Adrian83 commentedOK, I updated to dev and the twig help is working. Thank you. I'm still trying to figure out my question in #4.
Comment #8
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedThe challenge is that the Twig variables don't include formatted values, just the raw data, but you should be able to loop through multiple values.
Comment #9
Adrian83 CreditAttribution: Adrian83 commentedAre you saying that I should be able to loop through multiple values of this:
{{ webform_token('[webform_submission:values:option:entity:title]', webform_submission) }}
? Or were you talking about Twig variables?Comment #10
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedYou have to use the twig variable because webform_token() always returns renderable markup.
Comment #11
Adrian83 CreditAttribution: Adrian83 commentedThanks, I'll post once I have the for loop figured out.
Comment #12
Adrian83 CreditAttribution: Adrian83 commentedProgress report: So far I have these two things working.
I can loop through the entity IDs like this:
I can get a specific entity ID like this:
<p>{{ data.choose_opt.0 }}</p>
Still working at making the jump to the entity and its title. This isn't working:
<p>{{ data.choose_opt.0.entity.title }}</p>
nor this
<p>{{ data.choose_opt.0.entity.title.value }}</p>
I'll post when I have anything new.
Comment #13
Adrian83 CreditAttribution: Adrian83 commentedWith some help from markconroy in the Drupal Twig Slack, here is what I did to print the title of entities referenced earlier in the form. I used twig tweak to load a a media entity view mode which I stripped of all its wrappers. Here is the twig markup:
choose_opt
element.Result: On page 1 of the form, user selects several options in an Entity checkboxes element. On page 2 of the wizard, the user sees a summary of what had been selected on page 1. The nice thing is that this method would allow us to load any fields from the referenced entity simply by dragging more fields into the view mode.
Comment #14
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedWe need to add a link to install the Twig Tweak module.
I am going to reopen this ticket so that I make the change.
Comment #16
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #17
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #19
milos.kroulik CreditAttribution: milos.kroulik commentedThe solution in https://www.drupal.org/project/webform/issues/2926806#comment-12363523 is probably too complicated in most cases. Twig Tweak also supports printing field values from referenced entities directly like this:
I'm going to modify this code to send submission to selected people, represented by nodes.
Comment #20
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedTwig Tweak++