Just another 陰陽 site

Jacob Rockowitz: Reusing Webform elements, behaviors, and APIs within a Drupal configuration form


In my previous blog post about a few of my favorite Webform things, I expressed my appreciation for the fact that that “most of the code within the Webform module is reusable and extendable.” Writing that blog post helped me re-organize and improve my presentation titled, Webforms for Everyone. While updating my presentation, I found explaining and showing how the Webform module’s APIs are reusable is very challenging. As the Webform module has grown and gotten more complicated, I have continually improved the examples and demos included in the module. These examples have helped developers understand how to extend the Webform module. As a result, I decided the best way to show how the Webform module’s APIs – specifically form elements and behaviors – are reusable, was to create an example.

The Webform Custom (Configuration) Form example

The goal of the Webform Custom (Configuration) Form example is to show how to leverage Webform APIs outside of the Webform module. For instance, webform elements can be used within a Drupal configuration forms. A very basic reusable enhancement is attaching the Webform module’s JavaScript to warn users about unsaved changes.

A more advanced example is reusing the Webform module’s multiple element widget to collect rich data. Recently, I had to create an arbitrary list of node ids stored within a configuration file. In doing so, I was able to use Drupal’s entity autocomplete element combined with the Webform module’s multiple element support.

The below example shows how to use a multiple entity autocomplete element with the unsaved changes behavior within a configuration form.

The above example was created using the below code snippet.

Using a code generator to create examples

Code generators like Read More