3

I need to add fields to a node creation/editing form programmatically. More specifically, I need to take already-generated form elements from another form and insert them into the node submission form.

That part isn't what's hanging me up though. My problem is that I can't figure out how to make the database hang onto the values submitted via the new field.

For example, the following code (copypasta'd directly from the API Quickstart page) creates a form element inside the existing node form just fine, but when I enter a value and submit the form, the value is not stored.

function [mymodule]_form_alter(&$form, &$form_state, $form_id) {
    $form['foo'] = array(
      '#type' => 'textfield',
      '#title' => t('bar'),
      '#default_value' => t('foo'),
      '#size' => 60,
      '#maxlength' => 64,
      '#description' => t('baz'),
    );
}

How do I alter an existing form in such a way that the submitted values 'take,' so to speak?

3

Add something like this to your hook_form_alter().

$form['#submit'] += array('mymodule_another_submit_handler');

Create that function and save your values from $form_state['values'].

function mymodule_another_submit_handler(&$form, &$form_state) {}

Where are they supposed to go? You might try to fire the submit handler of the other form so you wouldn't have to write so much code.

  • Thanks! I'd like to save them as part of the node, as it would save fields that were entered on the content type editing interface. – beth Jun 13 '12 at 17:44
  • another node type, right ? – mojzis Jun 13 '12 at 17:49
  • I just want to save the input as part of the node that's created by the node creation form upon which these custom fields appear. Also, does the function my_module_my_form_submit() declaration go inside hook_form_alter(), or outside of it? – beth Jun 13 '12 at 17:53
  • so every node will have a different fieldset, even though they are the same type ? I am not sure you can acheive that ... Did you consider conditional fields ? (drupalmodules.com/module/conditional-fields) . And the new function would go outside - as any other function in your module. – mojzis Jun 13 '12 at 17:59
  • I'm not using fieldsets, but yes they would all have different fields with different values even though they are the same type. I don't think conditional fields offers integration with fields that are on a different node, does it? – beth Jun 13 '12 at 18:04
2

I hacked together a way of doing this. I have NO IDEA whether this is at all a good idea, or if I'm doing something blatantly terrible, so any code or ideas here are for information purposes only!

So, here's what I did (for a module called "webformta")

  1. On my content type "Answer Key" I added a text field with unlimited multiple values enabled.
  2. Inside webformta_form_alter() I add my fields:

        $form['foo'] = array(
          '#type' => 'textfield',
          '#title' => t('bar'),
          '#default_value' => t('foo'),
          '#size' => 60,
          '#maxlength' => 64,
          '#description' => t('baz'),
        );
     # ... and so forth for all the form elements as I want to add. 
    
  3. Then, after I add the form elements I want, still inside webformta_form_alter():

        $form['#submit'][] = 'webformta_answerkey_submit';
    
  4. Next, a separate submit function:

(This appends the responses given onto the ones already in the field without overwriting or deleting the old ones because the [] after $form['#submit'] above adds this onto the $form['submit'] array instead of replacing it.)

function webformta_answerkey_submit($form, &$form_state) {
    $foo = $form_state['input']['foo'];
    $values = $form_state['values']['field_answer_answers']['und'];
    $count = 0;
    foreach ($values as $value){
        $count += 1;
    }
    $form_state['values']['field_answer_answers']['und'][$count]['value'] = $foo;
    $form_state['values']['field_answer_answers']['und'][$count]['_weight'] = $count;
}

So, the values entered by your user in as many fields as you want can be stored in an array as one field with many values on the node submission form.

Anyone know if this is a terrible idea for any reason?

0

Your code just defines a form element. If you want to put the submitted value into the DB you have add a submitthandler and write appropriate code for it.

Maybe you consider adding a field to this node type as this does all the DB numbo jumbo for you for free. No coding would be required at all.

  • Yes, I see that it just defines a form element. That is my problem. How would I add a submit handler to an already-extant node submission form? All the examples I have found show only how to make a new one from scratch. I am doing it in code because I need to add different elements to different forms depending on the value of other forms. – beth Jun 13 '12 at 17:24
  • 1
    The submit handler field of the form takes an array, the submit handlers are called in the order of the array. – BetaRide Jun 13 '12 at 17:28
  • Is there documentation on the details of how to do that? I can't find anything except a tutorial for Drupal 5 (I'm using 7) – beth Jun 13 '12 at 17:38
  • Not sure, have a look at the implementation of the login form. – BetaRide Jun 13 '12 at 17:40

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.