6

Drupal 7, Webform 3

I want to programmatically submit a webform via drupal_form_submit. However, I'm having difficulty finding a $form_state structure that works. I've submitted my webform via the ui and examined the $form_state variable in my validate callback function, but I still can't get it to work.

My webform has one textfield named "first_name". Below is a snippet of code I've used to test. Whenever I run it, $form_errors is null. With no errors, I would expect the code to have worked. However, when I check the webform results, it says there are no submissions.

<?php
$form_id = 'webform_client_form_55805';
$form_state = array(
  'values' => array(
    'submitted' => array(
      'first_name' => 'David',
    ),
  ),
);
$node = node_load(55805);
$submission = (object) array();

drupal_form_submit($form_id, $form_state, $node, $submission);
$form_errors = form_get_errors();
?>

Am I doing it wrong? Is there a better way? Should I be using webform_client_form_submit instead?

I looked at this Q&A, but it doesn't work for me and seems a bit convoluted: How to submit webform programmatically with Ajax?

  • why do you need to programmatically submit the webform? Seems to me the whole point of Webform is to automate the form and submission for you... if you need greater control over the submission, what about using the Form API and create your own form programmatically in a custom module then process it according to your needs? – longboardnode Jun 20 '13 at 5:23
  • @longboardnode Because the user needs to be able to submit any pre-existing webforms. If the user is anonymous, the webform submission is cached and the user is shown the login form in a modal box. Once the user authenticates, the webform is programmatically submitted. I'm looking for two things: 1) Is drupal_form_submit what I should be using to programmatically submit a webform, if not, what should I be using? 2) What is the magical $form_state structure to make it work? – David Jun 20 '13 at 12:32
5

I used PhpStorm and xdebug to step through the form.inc code and finally found the correct structure for my $form_state. The structure I posted was close, just needed to add the 'op' => t('Submit'). See my final working code below.

To answer my questions:

  1. Yes, drupal_form_submit is what I should be using.
  2. See code below for the magical $form_state structure.
<?php
$form_id = 'webform_client_form_55805';
$form_state = array(
  'values' => array(
    'submitted' => array(
      'first_name' => 'David',
    ),
    'op' => t('Submit'),
  ),
);
$node = node_load(55805);
$submission = (object) array();

drupal_form_submit($form_id, $form_state, $node, $submission);
$form_errors = form_get_errors();
?>
  • I haven't checked your code if it works, but I upvoted both, your Q and A, to show my appreciation for your taking time to answer your question. Thanks, that often helps others. – Artur Jun 20 '13 at 21:57
  • Thanks. I have exactly the same issue: I needed to have an anonymous user fill in a couple of forms and then register them and then create the webform submissions using the data previously acquired. Must admit I was concerned it was going to be a major hassle but you've already done the hassle bit :-) appreciated. – Adaddinsane Oct 18 '13 at 7:26
0

I had to make a slight amendment to David's answer above (I can't comment as I don't have enough points). The data was not submitting as the op didn't match the text on the submit button. I have correct by reading it in from the webform info attached to the node.

<?php
$node = node_load(55805);
$form_id = 'webform_client_form_55805';
$form_state = array(
  'values' => array(
'submitted' => array(
  'first_name' => 'David',
    ),
    'op' => empty($node->webform['submit_text']) ? t('Submit') : t($node->webform['submit_text']),
  ),
);

$submission = (object) array();

drupal_form_submit($form_id, $form_state, $node, $submission);
$form_errors = form_get_errors();
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.