5

I have a form, automatically generated by a content type. I want to run a form_submit function to input/change some data to get saved in the database.

The content-type already has these fields setup, but I want them to be determined in a form_submit function rather than via user input.

I can get a form submit function, but I don't know how to add to the array of values being submitted without having to write the whole query myself. What variables do I need to grab and how to alter them to do this?

To be more specific.. in the form_alter, this does what I want. $form["field_org_id"]["und"][0]['value']['#default_value'] = $org->id;

But how do I do the equivalent out of site in the form_submit? (this is a content-add form and hoping not to rewrite the entire query)

23
  1. You have to use hook_form_alter or hook_form_FORM_ID_alter
  2. Add your submit handler to $form['#submit'] like this $form['#submit'][]='my_submit'
  3. And write your code:

    function my_submit($form, &$form_state) { 
      // your code
    }
    
| improve this answer | |
  • 1
    isn't there an auto generated form_submit hook name? I was trying that but wasn't working – Damon Jun 6 '12 at 21:57
  • 1
    this is taken from (api.drupal.org/api/drupal/…) Description: A list of custom submit functions that will be called when the element is submitted. This is usually to add additional submit functions to a form, or to use an alternate function rather than the default form validation function which is the form ID with _submit appended to it. – jibran Jun 6 '12 at 22:05
  • ok i have the hook working either though none of the ways that are supposed to work were working. But my real concern is how to write the function itself.. if I have $form["field_org_id"]["und"][0]['value']['#default_value'] = $org->id; in my form_alter.. how would I implement that same effect in my form_submit – Damon Jun 7 '12 at 14:22
  • 1
    it seems like you are working with node_submit for this have a look at hook_node_submit – jibran Jun 7 '12 at 17:56
3

The FORM_FUNCTION_submit() function fires automatically when the form with the prefix generator function is submitted:

function generate_special_form(){ // this function name is "FORM_FUNCTION" above
    $form = array();
    $form['field_org_id'] = array(
        '#type' => 'textfield',
        '#title' => 'Organisation',
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Submit',
    );
    return $form;
}

When that function submits it will automatically call the function generate_special_form_submit() with 2 parameters, you can get the submitted values like this:

function generate_special_form_submit($form, &$form_state){
    // Form above was submitted, now lets clean up $form_state['values'], 
    // that's the submitted values in an associative array.
    form_state_values_clean($form_state);
    // And here is the value that was submitted:
    drupal_set_message($form_state['values']['field_org_id']);
}

It looks like you're trying to modify a form that is generated in core. If that's the case, use hook_form_alter to add the $form['#submit'] value as Jibran outline above.

| improve this answer | |

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.