Drupal 7 – Create a webform programmatically

The following piece of code demonstrate how to create a webform programmatically.

$node = new stdClass();
$node->type = 'webform';
node_object_prepare($node);
$node->title    = 'A webform';
$node->language = LANGUAGE_NONE; // Or other language.
$node->body[$node->language][0]['value']   = '<p>This is the a webform.</p>';
$node->body[$node->language][0]['format']  = 'full_html';
$node->uid = 1;     // Set admin as author
$node->promote = 0; // Do not put this node to front page.
$node->comment = 1; // Closed the comment.

// Create the webform components.
$components = array(
  0 => array(
    'name' => 'First name',
    'form_key' => 'first_name',
    'type' => 'textfield',
    'mandatory' => 1,
    'weight' => 1,
    'pid' => 0,
    'extra' => array(
      'title_display' => 'before',
      'private' => 0,
    ),
  ),
  1 => array(
    'name' => 'Last name',
    'form_key' => 'name',
    'type' => 'textfield',
    'mandatory' => 1,
    'weight' => 2,
    'pid' => 0,
    'extra' => array(
      'title_display' => 'before',
      'private' => 0,
    ),
  ),
  2 => array(
    'name' => 'Email address',
    'form_key' => 'email_address',
    'type' => 'email',
    'mandatory' => 1,
    'weight' => 3,
    'pid' => 0,
    'extra' => array(
      'title_display' => 'before',
      'private' => 0,
    ),
  ),
  3 => array(
    'name' => 'Message',
    'form_key' => 'message',
    'type' => 'textarea',
    'mandatory' => 1,
    'weight' => 4,
    'pid' => 0,
    'extra' => array(
      'title_display' => 'before',
      'private' => 0,
    ),
  ),
);

// Setup notification email.
$emails = array(
  0 => array(
    'email' => 'abc@def.com',
    '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);

Done =)

Reference:

About these ads

2 thoughts on “Drupal 7 – Create a webform programmatically”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s