0

I'm using Drupal 7 and the Entityform module to build a form for people to fill in on my website.

This form will be filled in by a parent and contains some data about his child. There will be 2 buttons:

  • 1 for submitting the form data (default submit button/function)
  • 1 for submitting the form data + opening a new form (of the same type) for a 2nd child (custom submit button/function).

Since this 2nd child will have a lot of data (address, last name,...) that's the same as the first child I'd like to already have those fields filled in in the 2nd form (by using field values from the first form). The same can be repeated for a 3rd child, a 4th child,...

This is my code so far:

function extra_inschrijving_form_alter(&$form, &$form_state, $form_id){

    if ($form_id == 'generiek_inschrijvingsformulier__entityform_edit_form'){
        $form['submit2'] = $form['actions']['submit'];
        $form['submit2']['#value']='Inschrijven en ook een broer inschrijven';
        $form['submit2']['#weight']=100;
        $form['submit2']['#submit'][1]='extra_inschrijving_default_wp_submit';      
    }
}

This code added another submit-button and attached a submit handler (this works).

function extra_inschrijving_default_wp_submit($form, &$form_state){
   $values = &$form_state['values'];

   $prepopulates = array(
      'edit[field_entity_reference][und]' => 
            $values['field_entity_reference']['und'][0]['target_id'],
      'edit[field_1][und][0][value]' => $values['field_1']['und'][0]['value'],
      'edit[field_2][und][0][value]' => $values['field_2']['und'][0]['value'],
   );

   $form_state['redirect'] = array(
     'node/11',
     array(
        'query' => $prepopulates,
     ),
  );
}

Here I copy some field values from the form that will be submitted into the new form that has to be filled in. The parent should still be able to change the fields if he wants to. Both the normal fields and the entity reference field (select list) are copied/prepopulated successfully, but if the parent changes the entity reference field on the new form it still contains the old, prepopulated value when he submits it (although visibly something else is selected in the list).

  • um... I just want to mention that the module naming convention of Drupal 7 is using _ instead of camel-case. – Jimmy Ko Jul 23 '16 at 22:31
  • Thanks for the info, I'm pretty new to writing custom code. I changed it so it follows convention. – Robin De Haes Jul 23 '16 at 23:09
0

If you don't mind using query parameter for passing value. Prepopulate would be your choice.

Example code:

function extra_inschrijving_default_wp_submit($form, &$form_state){
  $values = &$form_state['values'];
  $prepopulates = array(
    'edit[title]' => $values['title'],
  );
  $form_state['redirect'] = array(
    'node/add/article',
    array(
      'query' => $prepopulates,
    ),
  );
}

$prepopulates is an array for query parameter which will pass the pre-filled values to the node add form. The array keys specify the fields prepopulate to. You can learn how to set the key from the handbook page.

At this moment, the latest stable version 2.1 has a known bug and it is discussing here. If you the module is not working on some field, you may need to add the patch in the thread. I don't recommend the older stable version because 2.1 has fixed an exploit.

  • Thanks a lot! This seem to work for most fields, except my entity reference field. I've put this entity reference in a select list, but it is unchangeable after the first submit (so it's always the same for all the children, even if you change the selection). The other fields are changeable when they've been prepopulated. Well, the entity reference field is changeable too, but on submission it still has the old, prepopulated value. I use this code for the entity reference: 'edit[field_entity_reference][und]' => $values['field_entity_reference']['und'][0]['target_id'], – Robin De Haes Jul 24 '16 at 15:56
  • How many field you need to pass to? Please be aware that there is a length limit on URL. For the problem on entity reference, could you update it into question? – Jimmy Ko Jul 24 '16 at 16:26
  • 5 or so, but length doesn't seem to be a problem. I've updated my question. – Robin De Haes Jul 24 '16 at 16:53
  • @RobinDeHaes It seems quite hard... I spent some time to try but no luck. I would suggest raising this issue in the issue thread and see if others can update the patch. I investigate into it a few days later when I am free. – Jimmy Ko Jul 26 '16 at 0:06
  • I'll raise the issue (and choose your answer as the answer by the end of the week, since it solved most of my question) – Robin De Haes Jul 26 '16 at 8:18

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.