Advanced Webforms

http://bit.ly/advanced-webforms

Jacob Rockowitz

Tests Confirm Expectations

Webform tests confirm rendering, processing, validation, settings (labels and layouts), and access.

He had “Webform features” marked as deprecated on a slide he didn’t talk about.

Having easy-to-repeat manual test is okay.

“Some tests” are better than no tests.

Webform has a lot of deprecated You should only write PHPUnit tests.

I have 217 forms that automatically get installed to test webform functionality.

modals doesn’t have an automated test yet, it’s javascript and haven’t done enough of them– but has a confirmation modal test form so he can go write to it and test manually if someone says there’s an issue.

Everything in Drupal 8 should be an entity or a plugin.

Webform is a config entity. Webform submission is a content entity.

Webforms do not use Field API. Submissions use an entity-attribute-value model (EAV) which provides a simple way to stor a lot of data.

He goes to devel/entity/info

and the webform devel module adds an api tab to contact.

entity_type and entity_id are the source entity.

evaluation webform, good for a college application

all new code from scratch in D8

it takes what comments does; comments can track what entity they are on, and comments can be on anything.

Always look at the Interface class.

Commerce has to do a custom plugin to see what cart someone is in.

Understanding Form API (FAPI). Render arrays are the basic building blocks of Drupal content– everything you see on a Drupal site.

element input - an element that accepts data a composite is a group of elements (like checkbox and radios) form a collection of elements and inputs

build validate submit

key concept behind any form

i started learning form API from the site settings form in Drupal– simple.

Going back to our friendly devel module, a tool to look at every element installed in Drupal.

Examples module is another great place to look at.

(lots more resources linked in the talk)

Composite elements must always use #tree

Webform example modules includes examples of elements, composits– there’s lots of examples.

Example: Webform Element

Element/WebformExampleElement.php

There’s a lot of elements already, so you’re more likely to be building your own composite element.

Here’s a first name, last name, date of birth and gender (oh dear should not have included that– was a dropdown though and we never saw the options, so good)

Processing data: Webform handler plugins

common- remote post, send it to another server and e-mail

it contains methods that act like hooks, something that captures behavior that is happening can react to a submission’s state. Drafts are a submission state, publish, delete, tec.

Supports conditional logic.

You can override settings in your WebformHandler to tweak default behaviors.

You can alter forms and elements: WebformHandler::alterElement

DebugWebformHandler is a good startinp point because it’s very simple.

Go to WebformHandlerInterface.php to see what you can extend.

preCreate, postCreate, all can be in this one plugin.

There’s an example for that too:

webform_example_handler module

(there are five example modules in webform module, at modules/)

You can extend TableWebformPLugin and get really fancy.

Saved hooks for last because they are the simplest to talk about.

Handrels are only applied to a single form. Hooks can be applied to all forms. All entity hooks are applicable to webforms.

Plugins & Event Subscribers are the “new” hooks for Drupal 8.

yourmodule_webform_submission_form_alter and yourmodule_webform_element_alter (?) can do similar things

hook_webform_handler_invoke_alter – captures any request going to a handler and lets you manipulate it. Say you need to massage data before you send it to a remote.

http://jrockowitz.com

For webform support, Drupal Slack #WebForm

for general questions, Drupal Answers is the way to go

For bugs, use the issue queue

Q: Don’t have to s

How not to store data in Drupal? For instance in health care, you don’t want the data being stored in Drupal.

In the specific webform’s settings: Disable saving of submissions. (It gives a warning )

stores it in state system and sends it out