I am building a module that will create webforms for use with the Commerce Webform Order module. These must be somewhat constrained in their structure, so I have developed a form (using FAPI) to configure them.

Well, the form has become rather complex, with several "add another" fields and the like. The Webform module handles these rather elegantly, and I'd love to use its capabilities for my module's config page, but feel that a traditional webform would be subject to error and fiddling.

The config page must look at a given webform (if specified) and use its content to pre-load values, or create fields, to allow for further settings adjustment (e.g. prices of items, additional fields, etc)

So, is it feasible to somehow invoke the webform module to create something that works like a webform but is under the control of my module?

Thanks in advance.

  • Building on @jrockowitz' great answer, I am finding this to be a great solution. Some elements require one to add "webform_" to the field type. One issue: in using webform_custom_composite, I am having trouble. The '#element" items, which render as a "<td>" on a webform, instead render as a "<div>" on my custom form. Adding works great, but I'd love these to be inline td's in the table row. Any thoughts? – Michael D. Moradzadeh Jan 24 '20 at 16:58

The Webform module just extends Drupal's Form API. Most of the elements available to a webform can be used in custom-built forms. For example, you can use the WebformMultiple element in any FAPI form.

@see https://git.drupalcode.org/project/webform/blob/8.x-5.x/src/Element/WebformMultiple.php

  • Hold on a minute: Dou you mean I can use every form element of Webforms for my own, custom class FooForm extends FormBase? (At least for the buildForm() part) – Hudri Jan 23 '20 at 11:46
  • 4
    Yep, almost all of them are reusable. One immediate exception is computed elements that rely on a webform submission. Select other, entity references, multiple, and composites are reusable. – jrockowitz Jan 23 '20 at 16:03
  • Wow, that's awesome, didn't know this was possible. This basically is a super-charger for core forms API, this will be very useful for my future work :-) – Hudri Jan 23 '20 at 16:59
  • 1
    Btw, this deserves a post on your personal blog ;-) – Hudri Jan 23 '20 at 17:01
  • 1
    To make the YAML Source editor easier, I remove the 'webform_*' suffix from all Webform specific elements. – jrockowitz Jan 23 '20 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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