0

I would like to output a submitted value as a token on the confirmation page through the twig. The token works just fine when added into the message box of webform as:

[webform_submission:values:score_percentage]

But I can't seem to get the same value to output through the twig. I have tried:

{{ webform_token('webform_submission:values:score_percentage') }}
{{ webform_token('[webform_submission:values:score_percentage]') }}
{{ drupal_token('webform_submission:values:score_percentage') }}
{{ drupal_token('[webform_submission:values:score_percentage]') }}

But they all just print the token itself, like:

webform_submission:values:score_percentage
[webform_submission:values:score_percentage]
[webform_submission:values:score_percentage]
[[webform_submission:values:score_percentage]]

I saw another post about (what I think is) the same thing here? But I haven't been able to see my error...

As well as this old issue, which by the looks of it would be fixed now.

I have the latest versions of Twig Tweak (8.x-2.9) and Token (8.x-1.9) installed. Am I doing something fundamentally wrong here?

2 Answers 2

2

For the code that implements webform_token, you can check the src/Twig/WebformTwigExtension.php file, line 102.

public function webformToken($token, EntityInterface $entity = NULL....) {
  // Allow the webform_token function to be tested during validation without
  // a valid entity.
  if (!$entity) {
    return $token;
  }
  // …
}

If you do not pass an entity, the string passed in, for example, {{ webform_token('webform_submission:values:score_percentage') }} will be returned.

If you look at the templates/webform-confirmation.html.twig file, you can see the following variables are available.

  • progress: Progress bar
  • message: Confirmation message
  • back_url: URL to the previous webform submission

The other available variables can be found on template_preprocess_webform_confirmation().

  • webform
  • webform_submission
  • source_entity

It means you can use {{ webform_submission.data.field_name }} in the webform-confirmation.html.twig file, instead of using tokens as you did.

Try to avoid using Twig extensions in template files. It will make debugging and maintenance easier.

-1

You need to install the Twig Tweak module and use {{ drupal_token('site:name') }}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.