function drupal_execute

You are here

5 form.inc drupal_execute($form_id, $form_values)
6 form.inc drupal_execute($form_id, &$form_state)

Retrieves, populates, and processes a form.

This function allows you to supply values for form elements and submit a form for processing. Compare to drupal_get_form(), which also builds and processes a form, but does not allow you to supply values.

There is no return value, but you can check to see if there are errors by calling form_get_errors().

// 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

File

includes/form.inc, line 339

Code

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'];

  // Reset form validation.
  $form_state['must_validate'] = TRUE;
  form_set_error(NULL, '', TRUE);

  drupal_prepare_form($form_id, $form, $form_state);
  drupal_process_form($form_id, $form, $form_state);
}

Comments

Note that in Drupal 7, this function was renamed drupal_form_submit(), which more accurately describes what it does.

When you want to execute a form in D6, you may have to include the module using module_load_include().

Also, I found this documentation page very helpful:

Configure Settings using drupal_execute

Josh

Nasty little bug. Talk about figuring out the hard way ;-(

Since drupal_execute eventually fires off drupal_validate_form, the same applies here.

static cache: cannot call drupal_validate_form on the same form more than once

Thanks to a number of folks, there are patches (see link) and I believe that D7 has also been fixed.

After 4hrs of debugging i found your message.
Thanks.

If you're using this function to programmatically create a custom node, make sure you use the correct form_id. It should not be your implementation of hook_form().

Instead, you should use the form_id of NODETYPE_node_form, of course substituting NODETYPE for your custom node type.

From: http://drupal.org/node/178506#comment-895418 by: orangecoat-ciallella

Drupal 6 Example Code

$form_state = array();
module_load_include('inc', 'node', 'node.pages');  // new for Drupal 6
$nodeTmp = array('type' => 'story'); // a variable holding the content type

$form_state['values']['type'] = 'story'; // the type of the node to be created
$form_state['values']['status'] = 1; // set the node's status to Published, or set to 0 for unpublished
$form_state['values']['title'] = 'Some Node Title Here';   // the node's title
$form_state['values']['body'] = 'whatever body you might want, or else, leave blank'; // the body, not required

  // the username of the node's author, must be a valid user with access to create this node.
  // Otherwise, see (http://drupal.org/node/178506#comment-726479) above about using user_load() to create nodes as any user, even when not logged in
$form_state['values']['name'] = 'someusername';
$form_state['values']['op'] = t('Save');  // this seems to be a required value

// CCK field examples for CCK in D6
$form_state['values']['field_mydropdown']['value'] = 3;    // a select (dropdown, where 3 is the "option" value saved to the db
$form_state['values']['field_myuserref'][0]['uid']['uid'] = 'someusername';  // a user reference example
$form_state['values']['field_mycheckbox']['value'] = 1;  // a checkbox example
$form_state['values']['field_mynoderef'][0]['nid']['nid'] = '[nid:9]';  // a node reference example. In this case, 9 is referring to node 9

  // call the function to create the node, node_revisions, and CCK values.
  // Replace "story" with the name of your form / content type
$errs = drupal_execute('story_node_form', $form_state, (object) $nodeTmp);

  // if there were any validation errors, drupal_execute will return them, then you have a chance to do something
if (count($errs)) {
  // optional code here
}

... Drupal Execute doesn't return anything. Errors must be retreived using form_get_errors().

How to add taxonomy term to the node. I am using taxonomy heirarchical select in the cck form as made a cck field. So how to add this taxonomy cck hiecarachical select through drupal_execute function. I am using drupal 6.
I tried to add taxonomy term but didnot succeed. All others fields are working good. Thanks in advance for your help.

tried to solve it through many ways, but not seems there's a solution on forums...

one option is to create node and after use taxonomy_node_save() , but i need this taxonomy field as mandatory,

anyone could solve this ?

thank you in advance

I find that using the node_export_import_form is a nice way to add nodes to a site programatically.

  //create webform
    $form_state = array();
    $form_state['values']['code'] = _sponsored_ad_webform_definition();
    $form_state['values']['op'] = t('Import');
    drupal_execute('node_export_import_form', $form_state);

The sponsored_ad_webform_definition() function simply returns the array that the node_export module generates when exporting a node.

function _sponsored_ad_webform_definition() {
  $ret = "
 
  array(
  'nid' => '24238',
  'type' => 'webform',
  'language' => '',
  'uid' => '1',
  'status' => '1',
  'created' => '1316551143',
  'changed' => '1317222784',
  'comment' => '2',
  'promote' => '1',
  'moderate' => '0',
  'sticky' => '0',   ..... ";
  return $ret;
}