0

how can I add a field to a form submission upon submit that will have the same value as the field name "mail" but with the name "email"

2 Answers 2

0

I think it'll work if you implement hook_form_submit() hook for this form, and in that function set the value of one field to another.

E.g.,

FORM_ID_form_submit($form, &$form_state) {
  $form_state['values']['email'] = $form_state['values']['mail']; 
}

If your fields are fields of a content then replace the above line with this:

$form_state['values']['email']['und'][0]['value'] = $form_state['values']['mail']['und'][0]['value'];

3
  • the form submit when i put in this code doesnt seem to be reach. It goes to the page that I have marked as the form action and never gets there.
    – Diana
    Commented Apr 25, 2017 at 20:30
  • this doesnt work for drupal 8
    – Diana
    Commented Apr 25, 2017 at 21:44
  • My bad, didn't realize you were asking for Drupal8. Sorry can't help. Haven't looked into Drupal8 yet.
    – Amir
    Commented Apr 26, 2017 at 21:34
0

In Drupal 8 I did it this way, there may be a shorter way though

$value=$form_state->getValue('field_name');
$number=($value[0]['value']);
$form_state->setValue('other_field',$number);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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