简体   繁体   English

Drupal 9 Webforms - 使用 webform_token 并测试定义的字段

[英]Drupal 9 Webforms - using webform_token and testing for defined fields

We have 6-8 different webforms on our site which submit via ajax and display the conformation message inline.我们的网站上有 6-8 种不同的网络表单,它们通过 ajax 提交并内联显示确认消息。 I would like to put data from the form submissions into a JavaScript data layer so we can pass it on to Google Tag Manager.我想将表单提交的数据放入 JavaScript 数据层,以便我们可以将其传递给 Google 跟踪代码管理器。 I thought the most fool proof way to do this, and make sure it gets done on any new form going forward, would be to create a custom webform-confirmation.html.twig template and drop the fields we are interested in. Once there I am printing the submitted data by using the following TWIG:我认为最简单的方法是创建一个自定义的webform-confirmation.html.twig模板并删除我们感兴趣的字段。一旦到了那里,我就可以确保它在任何新表单上完成我正在使用以下 TWIG 打印提交的数据:

<script>
   window.dataLayer = window.dataLayer || [];
   window.dataLayer.push({
    'email': '{{ webform_token('[webform_submission:values:email]', webform_submission) }}',
    'timeline' : '{{ webform_token('[webform_submission:values: timeline]', webform_submission) }}',
   });
</script>

That works great for the email field as it is on every form and always required.这对电子邮件字段非常有用,因为它在每个表单上都是必需的。 However some forms have a “timeline” field and other do not.但是,有些表格有“时间线”字段,而其他表格则没有。 For forms that have timeline I expected对于具有我预期的时间表的表格

{{ webform_token('[webform_submission:values: timeline]', webform_submission) }}

Would just print nothing, but instead I get the following output:什么都不打印,但我得到以下输出:

[webform_submission:values:project_timeline]

I have tried everything I can think of in TWIG to test to see if the webform_submission:values: timeline is defined but am unable to get anywhere.我已经在 TWIG 中尝试了所有我能想到的方法来测试是否定义了 webform_submission:values: 时间线,但我无法到达任何地方。 Is this possible?这可能吗? Any ideas?有任何想法吗?

Drupal Version 9.4.9 Drupal 版本 9.4.9
Webform Version: 6.1.3网络表格版本:6.1.3

Thanks, summer谢谢,夏天

I ended up doing it as follows, feels like there should be a better solution but this is working....我最终按如下方式进行了操作,感觉应该有更好的解决方案,但这是有效的……

  {% set project_timeline= webform_token('[webform_submission:values:project_timeline]', webform_submission) %}

  {% if project_timeline!= "[webform_submission:values:project_timeline]" %}
    'project_timeline' : '{{ project_timeline }} ',
  {% endif %}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM