简体   繁体   English

Drupal 8 Webform:如何在下一页的一页上显示文本输入?

[英]Drupal 8 Webform: how to display text input on one page on the next page?

I am trying to develop a multistep webform in Drupal 8 using Webform 8.x-5.1.我正在尝试使用 Webform 8.x-5.1 在 Drupal 8 中开发多步 Web 表单。 I have written a WebformHandler that extends Drupal\\webform\\Plugin\\WebformHandlerBase and made it available to the webform.我编写了一个扩展Drupal\\webform\\Plugin\\WebformHandlerBase并使其可用于 webform。

In the first step of the webform, I collect a text-field.在网络表单的第一步中,我收集了一个文本字段。 I would like to display the value of that text-field in an HTML element ( Advanced HTML/Text or Basic HTML ) on the second page after doing some computation.在进行一些计算后,我想在第二页的 HTML 元素( Advanced HTML/TextBasic HTML )中显示该文本字段的值。

I have overwritten submitForm() in the WebformHandler and in it assign the value I want to the HTML element as follows:我已经覆盖了 WebformHandler 中的submitForm()并在其中将我想要的值分配给 HTML 元素,如下所示:

$form['elements']['page_name']
     ['advanced_html_element']['#text'] = '...my HTML...';

Using ksm() I can see that this assignment works, but the the HTML element is not rendered with my HTML: the element is either invisible or contains the initial value set up in the form editor.使用ksm()我可以看到这个分配工作,但 HTML 元素没有用我的 HTML 呈现:该元素要么不可见,要么包含在表单编辑器中设置的初始值。

Clearly I'm missing something.显然我错过了一些东西。 Should I be using something other than submitForm ?我应该使用submitForm以外的东西吗? Can anyone help me?谁能帮我?

It's been a long haul, but I've finally worked out how to do what I want to.这是一个漫长的过程,但我终于想出了如何做我想做的事。 The following works for me.以下对我有用。

Firstly, I discovered the method validateForm in WebformHandlerBase .首先,我发现了WebformHandlerBase validateForm方法。 On each page in a form with multiple pages, you will find that the following methods are called in the order given here:在包含多个页面的表单中的每个页面上,您会发现按照此处给出的顺序调用以下方法:

  • submitForm (called once) submitForm (调用一次)
  • alterForm (called possibly more than once) alterForm (可能多次调用)
  • validateForm (called once) validateForm (调用一次)

The name validateForm leads me to believe I may be misusing this method, but that is where I set up the elements on the following page that I wish to programmatically initialise.名称validateForm使我相信我可能会误用此方法,但这就是我在下一页上设置我希望以编程方式初始化的元素的地方。 It works, so what the hey!它有效,所以嘿!

In validateForm , I initialise the elements that appear on the following page as follows:validateForm ,我初始化出现在以下页面上的元素,如下所示:

$form_state->setValue(<element name>, <data structure>);

The <element name> is the name you give the element in the form editor ("Build" tab). <element name>是您在表单编辑器(“构建”选项卡)中为<element name>的名称。 The <data structure> has to be correct, of course: I suggest you find the appropriate structure by first filling in the element on the next page manually and seeing what turns up in $form_state . <data structure>当然必须是正确的:我建议您首先手动填写下一页的元素并查看$form_state中出现的内容,从而找到合适的结构。

There is also a $form_state->getValue(<element name>) , which seems to me to mean that $form_state can also be used for storing session data, say in hidden fields.还有一个$form_state->getValue(<element name>) ,在我看来这意味着$form_state也可以用于存储会话数据,比如在隐藏字段中。 I initially used Drupal::service('tempstore.private')->get('xxx') for storing data that had to be available across page boundaries, but $form_state might be a cleaner solution.我最初使用Drupal::service('tempstore.private')->get('xxx')来存储必须跨页面边界可用的数据,但$form_state可能是一个更$form_state的解决方案。

I hope this helps someone: I spent a horribly long time trying to get this to work.我希望这对某人有所帮助:我花了很长时间试图让它发挥作用。

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

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