drupal_execute

Versions
5
drupal_execute($form_id, $form_values)
6
drupal_execute($form_id, &$form_state)

Retrieves a form using a form_id, populates it with $form_state['values'], processes it, and returns any validation errors encountered. This function is the programmatic counterpart to drupal_get_form().

<?php

// register a new user
$form_state = array();
$form_state['values']['name'] = 'robo-user';
$form_state['values']['mail'] = 'robouser@example.com';
$form_state['values']['pass']['pass1'] = 'password';
$form_state['values']['pass']['pass2'] = 'password';
$form_state['values']['op'] = t('Create new account');
drupal_execute('user_register', $form_state);

// Create a new node
$form_state = array();
module_load_include('inc', 'node', 'node.pages');
$node = array('type' => 'story');
$form_state['values']['title'] = 'My node';
$form_state['values']['body'] = 'This is the body text!';
$form_state['values']['name'] = 'robo-user';
$form_state['values']['op'] = t('Save');
drupal_execute('story_node_form', $form_state, (object)$node);

?>

Parameters

$form_id The unique string identifying the desired form. If a function with that name exists, it is called to build the form array. Modules that need to generate the same form (or very similar forms) using different $form_ids can implement hook_forms(), which maps different $form_id values to the proper form constructor function. Examples may be found in node_forms(), search_forms(), and user_forms().

$form_state A keyed array containing the current state of the form. Most important is the $form_state['values'] collection, a tree of data used to simulate the incoming $_POST information from a user's form submission.

... Any additional arguments are passed on to the functions called by drupal_execute(), including the unique form constructor function. For example, the node_edit form requires that a node object be passed in here when it is called. For example:

Related topics

▾ 12 functions call drupal_execute()

cck_gallery_install in contributions/cck_gallery/cck_gallery.install
Implementation of hook_install()
content_copy_export in contributions/cck/modules/content_copy/content_copy.module
Process the export, get field admin forms for all requested fields and save the form values as formatted text.
content_copy_import_form_submit in contributions/cck/modules/content_copy/content_copy.module
Submit handler for import form. For each submitted field: 1) add new field to the database 2) execute the imported field macro to update the settings to the imported values
content_copy_service_import in contributions/deploy/services/content_copy_service/content_copy_service.inc
Content copy import service callback
domain_prefix_domainupdate in contributions/domain/domain_prefix/domain_prefix.module
Implement hook_domainupdate().
domain_user_user in contributions/domain/domain_user/domain_user.module
Implement hook_user()
editablefields_submit in contributions/editablefields/editablefields.module
Menu callback: ajax submit.
hs_content_taxonomy_common_config_form_submit in contributions/hierarchical_select/modules/hs_content_taxonomy.admin.inc
Additional submit callback to update the multiple values field setting.
multichoice_update in contributions/quiz/question_types/multichoice/multichoice.module
Implementation of hook_update().
pm_module_manage in contributions/drush/commands/pm/pm.drush.inc
uc_cart_complete_sale in contributions/ubercart/uc_cart/uc_cart.module
Complete a sale, including adjusting order status and creating user account.
uc_product_kit_nodeapi in contributions/ubercart/uc_product_kit/uc_product_kit.module
Implementation of hook_nodeapi().

Code

6/includes/form.inc, line 294

<?php
function drupal_execute($form_id, &$form_state) {
  $args = func_get_args();

  // Make sure $form_state is passed around by reference.
  $args[1] = &$form_state;
  
  $form = call_user_func_array('drupal_retrieve_form', $args);
  $form['#post'] = $form_state['values'];
  drupal_prepare_form($form_id, $form, $form_state);
  drupal_process_form($form_id, $form, $form_state);
}
?>