Programmatically creating a webform

Last updated on
30 April 2025

Webforms cannot be easily be exported like other component such as views, content types, contexts and variables, since they are basically considered as content (if you really want to follow this path, you can use UUID, or consider switching to Entity Forms, which adopts a different approach). However, you can still write code in a module or an install profile to create one. The following is an adaptation of the code found on this page.

EDIT: DuaelFr recently wrote Webform Features!

$node = new stdClass();
$node->type = 'webform';
node_object_prepare($node);
$node->title = 'Contact Us';
$node->language = 'en';
$node->body[LANGUAGE_NONE][0]['value']   = '';
$node->body[LANGUAGE_NONE][0]['format']  = 'full_html';
$node->uid = 1;
$node->promote = 0;
$node->comment = 0;

// Create the webform components.
$components = array(
  array(
  'name' => 'Gender',
  'form_key' => 'gender',
  'type' => 'select',
  'mandatory' => 1,
  'weight' => 0,
  'pid' => 0,
  'extra' => array(
    'title_display' => 'inline',
    'private' => 0,
    'items' => "Mrs|Mrs\nMiss|Miss\nMr|Mr",
    'aslist' => 1,
  ),
),
array(
  'name' => 'Last name',
  'form_key' => 'name',
  'type' => 'textfield',
  'mandatory' => 1,
  'weight' => 5,
  'pid' => 0,
  'extra' => array(
    'title_display' => 'inline',
    'private' => 0,
  ),
),
array(
  'name' => 'First name',
  'form_key' => 'first_name',
  'type' => 'textfield',
  'mandatory' => 1,
  'weight' => 10,
  'pid' => 0,
  'extra' => array(
    'title_display' => 'inline',
    'private' => 0,
  ),
),
array(
  'name' => 'City',
  'form_key' => 'city',
  'type' => 'textfield',
  'mandatory' => 0,
  'weight' => 15,
  'pid' => 0,
  'extra' => array(
    'title_display' => 'inline',
    'private' => 0,
  ),
),
array(
  'name' => 'Country',
  'form_key' => 'country',
  'type' => 'select',
  'mandatory' => 0,
  'weight' => 20,
  'pid' => 0,
  'extra' => array(
    'title_display' => 'inline',
    'private' => 0,
    'aslist' => 1,
    'options_source' => 'countries',
  ),
),
array(
  'name' => 'Email address',
  'form_key' => 'email_address',
  'type' => 'email',
  'mandatory' => 1,
  'weight' => 25,
  'pid' => 0,
  'extra' => array(
    'title_display' => 'inline',
    'private' => 0,
  ),
),
array(
  'name' => 'Subject',
  'form_key' => 'subject',
  'type' => 'select',
  'mandatory' => 1,
  'weight' => 30,
  'pid' => 0,
  'extra' => array(
    'title_display' => 'inline',
    'private' => 0,
    'items' => "s1|Subject 1\nother|Other",
    'aslist' => 1,
  ),
),
array(
  'name' => 'Message',
  'form_key' => 'message',
  'type' => 'textarea',
  'mandatory' => 1,
  'weight' => 35,
  'pid' => 0,
  'extra' => array(
    'title_display' => 'inline',
    'private' => 0,
    ),
  ),
  array(
    'name' => 'Mandatory Fields',
    'form_key' => 'mandatory_fields',
    'type' => 'markup',
    'mandatory' => 0,
    'weight' => 40,
    'pid' => 0,
    'value' => '<p>Fields with * are mandatory</p>',
    'extra' => array(
      'title_display' => 'inline',
      'private' => 0,
      'format'=> 'full_html',
    ),
  ),
);

// Setup notification email.
$emails = array(
  array(
    'email' => 'somebody@example.tld',
    'subject' => 'default',
    'from_name' => 'default',
    'from_address' => 'default',
    'template' => 'default',
    'excluded_components' => array(),
  ),
);

// Attach the webform to the node.
$node->webform = array(
  'confirmation' => '',
  'confirmation_format' => NULL,
  'redirect_url' => '<confirmation>',
  'status' => '1',
  'block' => '0',
  'teaser' => '0',
  'allow_draft' => '0',
  'auto_save' => '0',
  'submit_notice' => '1',
  'submit_text' => '',
  'submit_limit' => '-1', // User can submit more than once.
  'submit_interval' => '-1',
  'total_submit_limit' => '-1',
  'total_submit_interval' => '-1',
  'record_exists' => TRUE,
  'roles' => array(
    0 => '1', // Anonymous user can submit this webform.
  ),
  'emails' => $emails,
  'components' => $components,
);

// Save the node.
node_save($node); 

Handy tip: You can create the webform manually, then use the node_export module to export it. This provides data structures which can be cut-and-pasted into your code. It includes both the components and any conditionals you have set up.

Help improve this page

Page status: Not set

You can: