How to print token in webform-confirmation.html.twig?
Вопрос
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]]
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?
Решение
First, talking about your twig extension:
- {{ webform_token(....) }}
- {{ drupal_token(....) }}
I've never seen drupal_token twig ext so let it out of here. or you guys can comment to help me update this answer.
For webform_token function, the real back end function of it you can check from this file: webform/src/Twig/WebformTwigExtension.php
line 102. (v5.23.0). And it said that
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;
}
...bla...bla
}
Mean that, if your code not given entity, it will return a string that you inputed. and the result will be like what you did.
Back to your case
If you look into webform/templates/webform-confirmation.html.twig
template file, you can see the Available variables:
- progress: Progress bar.
- message: Confirmation message.
- back_url: URL to the previous webform submission.
And the others Available variables can found at template_preprocess_webform_confirmation()
of this file: webform/includes/webform.theme.template.inc
. And they are
- webform,
- webform_submission,
- source_entity.
So, it mean that you can use {{ webform_submission.data.field_name }} in webform-confirmation.html.twig
template file; instead of using token like the way you did above.
*Note: Try to avoid using twig extension in twig template would make you easier in debugging and maintenance.