Drupal Webform Module Setup for Forms
Webform is the most functional forms module for Drupal. Form builder with 50+ element types, conditional logic, multi-step forms, submission handlers (email, webhook, CRM), export answers to CSV/Excel.
Installation
composer require drupal/webform
drush en webform webform_ui -y
drush cr
Creating a Form
Structure → Webforms → Add webform. Name, machine name, description.
Form builder: Elements → add fields by dragging or button. Element types:
- Text: Textfield, Textarea, Email, Tel, URL, Number
- Choice: Select, Radios, Checkboxes, Buttons
- Date/time: Date, Time, DateTime
- Files: File, Managed file
- Complex: Address, Composite element, Signature
- Containers: Fieldset, Details, Section, Flexbox
YAML Element Configuration
Webform stores form structure in YAML, enabling git versioning:
# contact_form.webform configuration
elements: |
name:
'#type': textfield
'#title': 'Name'
'#required': true
'#maxlength': 100
email:
'#type': email
'#title': 'Email'
'#required': true
phone:
'#type': tel
'#title': 'Phone'
'#input_mask': '+1 (999) 999-9999'
message:
'#type': textarea
'#title': 'Message'
'#required': true
'#minlength': 10
'#rows': 5
department:
'#type': select
'#title': 'Department'
'#options':
sales: Sales
support: Support
other: Other
Conditional Logic
company_name:
'#type': textfield
'#title': 'Company Name'
'#states':
visible:
':input[name="client_type"]':
value: business
required:
':input[name="client_type"]':
value: business
Email Handler
Settings → Emails/Handlers → Add Email:
- To:
[webform_submission:values:email](dynamic from form) - Subject:
New submission from [webform_submission:values:name] - Body: use template with Drupal tokens
Webhook Handler
composer require drupal/webform_rest
drush en webform_rest -y
Or custom handler:
// src/Plugin/WebformHandler/CrmWebformHandler.php
namespace Drupal\mymodule\Plugin\WebformHandler;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\WebformSubmissionInterface;
/**
* @WebformHandler(
* id = "crm_integration",
* label = @Translation("CRM Integration"),
* category = @Translation("External"),
* description = @Translation("Sends submission to CRM"),
* cardinality = WebformHandlerBase::CARDINALITY_SINGLE,
* results = WebformHandlerBase::RESULTS_PROCESSED,
* )
*/
class CrmWebformHandler extends WebformHandlerBase {
public function postSave(WebformSubmissionInterface $webform_submission, bool $update): void {
$data = $webform_submission->getData();
\Drupal::httpClient()->post('https://crm.example.com/api/leads', [
'json' => [
'name' => $data['name'],
'email' => $data['email'],
'message' => $data['message'],
'source' => 'drupal-webform',
],
]);
}
}
Inserting Form on Page
{# In twig template #}
{{ drupal_entity('webform', 'contact_form') }}
Or via block: Structure → Block placement → Webform block.
Export Answers
Webforms → [form] → Results → Download. Formats: CSV, Excel, JSON. Supports filtering by date and status.
Timeline
Creating and configuring a form with email handler and basic conditional logic — 3–5 hours. Custom CRM integration handler — plus 1–2 days.







