Drupal 7: drupal_execute has Gone, Say Hello to drupal_form_submit

Aug
17
0

As has been commented elsewhere, when you’re programmatically inserting data the most reliable way of doing so is often to simulate form submission – that way, any validation rules are applied and other modules have a chance to intercept the submission to do what they need to with the data.

This was achieved using the function drupal_execute:

$form_state = array();
$form_state['values'] = array();
$form_state['values']['field1'] = $value1;
$form_state['values']['field2'] = $value2;
drupal_execute('your_form_id', $form_state);

However, drupal_execute has gone altogether from Drupal 7.

Instead, you need simply replace any call to drupal_execute with the new function drupal_form_submit (see the documentation). Otherwise, everything (from the point of view of your syntax) remains the same.

Don't forget to use form_get_errors() to ensure that the form has validated.

Posted

August 17th, 2011

Category

Drupal

Comments

no comments

 

No comments

Add your comment

The content of this field is kept private and will not be shown publicly.

About Me

Lukas White, LAMP Developer Manchester

I'm a web developer based in Manchester, England. I also design websites.  I generally favour open-source technologies such as PHP, MySQL and JQuery, and I have extensive experience using Drupal.

Read more »

Recent Comments

2 weeks 6 days ago kc6ape commented on Using an Additional Database in Drupal 7:

Lukas,
Thanks so much for posting this, could you possibly help a newbie out and show...

Read more...
6 weeks 1 day ago Anonymous commented on Using Less, the Dynamic Stylesheet Language, in Concrete5:

Hello,
I'm using bootstrap with Less .
I'm trying to get this package to work with...

Read more...
6 weeks 5 days ago Sean J commented on Using Less, the Dynamic Stylesheet Language, in Concrete5:

Thanks for the add-on! I'd love to use it but when installing I was told it was incompatible...

Read more...