1

I have a custom submit handler:

function submituserform(array $form, FormStateInterface $form_state) {
  $email = $form_state->getValue('field_email');
  drupal_set_message('Thank you for for submitting your application. You will receive a confirmation at ' . $email . ');

  $redirect_path = "/confirmation";
  $url = url::fromUserInput($redirect_path);
  $form_state->setRedirectUrl($url);

}

I get Array as value for the email. How do I get the email from field_email?

2
  • PHP Arrays, get good. Install devel and dpm($form_state->getValue('field_email')); to see the structure of the array.
    – No Sssweat
    May 23 '17 at 10:57
  • 2
    @NoSssweat Thank you. I added this: $value = $form_state->getValue('field_email'); $email = $value[0]['value'];
    – ernzkee
    May 24 '17 at 2:40
1

You can also do this in following way:

function submituserform(array $form, FormStateInterface $form_state) {
  $element = $form['field_email'];
  $email = $form_state->getValue($element['#parents']);

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.