logo

Programatically add content to webform DRUPAL

22
April

Programatically add content to webform DRUPAL
By: Anonymous | Published On: Wed, 04/22/2020 - 22:13

global $user;
$nid = 4; //nid is the node id of your webform.

$node = node_load($nid); 

The values to save. Take case about array indexes! (see below)

$data = array(
    '1' => array('0' => $type),
    '2' => array('0' => $method),
    '5' => array('0' => $volume),
    '6' => array('0' => $comment),
    '7' => array('0' => $phone),
    '8' => array('0' => $length)
);

$submission = (object) array(
    'nid' => $nid,
    'uid' => $user->uid,
    'submitted' => REQUEST_TIME,
    'remote_addr' => ip_address(),
    'is_draft' => FALSE,
    'data' => $data,
);
Include the necessary files.

module_load_include('inc', 'webform', 'webform.module');
module_load_include('inc', 'webform', 'includes/webform.submissions');
webform_submission_insert($node, $submission); // Submit a submission.
webform_submission_send_mail($node, $submission); // Send webform's e-mails

Need Help ?