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 }}.
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Adrian83 created an issue. See original summary.

jrockowitz’s picture

Try using {{ webform_token('[webform_submission:values:option:entity:title]', webform_submission) }}.

We need to better document the webform_token() Twig function in the UI.

jrockowitz’s picture

Status: Active » Needs review
FileSize
106.02 KB
1.53 KB

..and the inline Twig help was broken.

The attached patch fixes the issue.

Adrian83’s picture

@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 }}?

  • jrockowitz committed ccfb736 on 8.x-5.x
    Issue #2926806 by jrockowitz: How to print title of referenced entity...
jrockowitz’s picture

Status: Needs review » Fixed

I committed the patch. Please download the latest dev release to review.

Adrian83’s picture

OK, I updated to dev and the twig help is working. Thank you. I'm still trying to figure out my question in #4.

jrockowitz’s picture

The 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.

Adrian83’s picture

...you should be able to loop through multiple values.

Are 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?

jrockowitz’s picture

You have to use the twig variable because webform_token() always returns renderable markup.

Adrian83’s picture

Thanks, I'll post once I have the for loop figured out.

Adrian83’s picture

Progress report: So far I have these two things working.

I can loop through the entity IDs like this:

<ul>
{% for item in data.choose_opt %}
  <li>{{ item }}</li>
{% endfor %}
</ul>

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.

Adrian83’s picture

With 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:

{% set media_ids = data.choose_opt %}
<ul>
{% for media_id in media_ids %}
<li>{{ drupal_entity('media', media_id, 'title_as_string') }}</li>
{% endfor %}
</ul>
  1. I set a variable for the entity ID's stored in the choose_opt element.
  2. A for loop loops through the resulting array.
  3. Twig tweak allows us to load a Drupal entity.
  4. I specify the media entity type.
  5. My variable media_id passes in the media entity ID.
  6. 'title_as_string' is the machine name of a view mode that I've created for the media entity. I needed to remove a couple div wrappers by overriding both the field template and the entity template for this view mode.

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.

jrockowitz’s picture

Status: Fixed » Active

We need to add a link to install the Twig Tweak module.

I am going to reopen this ticket so that I make the change.

  • jrockowitz committed 814606e on 8.x-5.x
    Issue #2926806 by jrockowitz, Adrian83: How to print title of referenced...
jrockowitz’s picture

jrockowitz’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

milos.kroulik’s picture

The 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:

<ul>
{% for nid in data.vyberte_poslance %}
<li>
{{ drupal_field('field_email', 'node', nid)|without('#theme') }}
</li>
{% endfor %}
</ul>

I'm going to modify this code to send submission to selected people, represented by nodes.

jrockowitz’s picture

Twig Tweak++