Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

The media_mover module contains this function, which builds a form and pre-populates default values:

/* step 1 */ $form = media_mover_api_configuration_base_form($configuration); 

It also contains this function, which submits the form:

/* step 2 */ function media_mover_api_add_config_form_submit($form, &$form_state)

I would like to programmatically generate the form (step 1), and then programmatically submit the form (step 2).
Step 1 is not a problem; I am able to generate $form. For step 2, I need $form_state.

Given $form, is there a way to programmatically generate its corresponding $form_state? I am using Drupal 6.

This is a more detailed explanation of what I am trying to accomplish. I am able to programmatically fill out and submit a particular form by laboriously setting all the field values in $form_state['values'], and then calling media_mover_api_add_config_form_submit($form, &$form_state);

However, the process would be much simpler if I could instead use this function, which converts a $configuration object into $form, and does all the heavy lifting:

$form = media_mover_api_configuration_base_form($configuration);

This function enters a default value into each form field. The only missing step, then, is to copy all these default field values from $form to $form_state['values']. I am wondering whether there is a function to do this. Or is there a way to loop through all the fields in $form and extract the field-name and default value?

share|improve this question
add comment

4 Answers

up vote 1 down vote accepted

Here is what I did to fill $form_state['values'] with the default values from $form:

function mymodule_get_form_state($form){
  $values = array();
  _mymodule_form_get_values($form, &$values);
  $form_state = array();
  $form_state['values'] = $values;
  return ($form_state);
}

function _mymodule_form_get_values($element, &$values) {
  foreach (element_children($element) as $key) {
    if (isset($element[$key]) && $element[$key]) {
      if (isset($element[$key]['#default_value']) && $element[$key]['#default_value']) {
        $values[$key] = $element[$key]['#default_value'];
      } else {
        // Recurse through all children elements.
        _mymodule_form_get_values($element[$key], &$values);
      }
    }
  }
}
share|improve this answer
add comment

FAPI is your friend and embeds this functionality.

share|improve this answer
add comment

form_state_defaults() will retrieve the default values. But what are you exactly trying to achieve? I'm guessing that at some point you want to set some values to be submitted? Do you have post data?

As paul-m I'm also guessing that media_mover_api_add_config_form_submit() is a function registered as the submit function for the form you mention. Calling this function as above won't work.

You can call the process form function though, which will build it validate and submit it if applicable.

ie: drupal_process_form($form['#form_id'], $form, $form_state);

share|improve this answer
    
Please see my clarification above, for what I am trying to accomplish. Basically: Is there a way to copy the default values from $form to $form_state['values'] –  user606696 Mar 1 '12 at 19:58
add comment

I would wager that media_mover_api_add_config_form_submit() is a function registered as the submit function for a form. That is, it doesn't 'submit the form,' it is called when the user submits the form in their web browser.

Form API lets you register functions to handle validation and submission of forms. Generally, you don't call these; you let Drupal do it.

I suppose the obvious question is: Why do you need $form_state?

Here's a link to the Form API Quickstart Guide.

UPDATE: OK given your update....

I know very little about Media Mover, though it looks useful. Poking through the code, I suggest that if you need to save a new configuration, then you just do it: $conf = new media_mover_configuration; /* set configuration here */ $conf->save(); Or subclass the configuration if you need an encapsulated approach. You also might try asking the project's lead for guidance, and remind them to add more documentation to the code. :-)

Also, MM might support Features, if that helps you.

share|improve this answer
    
Yes, I believe you are right, that the intended use of media_mover_api_add_config_form_submit() is to submit the form when the user submits the form in their web browser. I was hoping I to use the same function to programmatically submit the form. The reason I need $form_state is that this submit function looks at $form_state, rather than $form. –  user606696 Mar 1 '12 at 6:46
    
Please see my clarification above, for what I am trying to accomplish. Thank you. –  user606696 Mar 1 '12 at 19:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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