I've done a custom contact form with 2 more fields, but I can't get values of those 2 custom fields.

I modified contact.module in core to see what are coming from form submit, found my custom fields but they are empty. I just don't know why they are empty.

Value of contact_form in contact.module:

$contact_form = {"contact_form":[{"target_id":"feedback"}],"uuid":[{"value":"5470b680-99db-4d5a-8c00-ae043dce67e1"}],"langcode":[{"value":"pt-br"}],"name":[{"value":"Name"}],"mail":[{"value":"some@mail.com"}],"subject":[{"value":"Any"}],"message":[{"value":"Any message"}],"copy":[{"value":0}],"recipient":[],"field_customa":[],"field_customb":[]}

Any clues to make this contact form work?

  • I think the values are in a Message entity (defined in contact module). It seems as if the entity is passed straight into the parameters for the mail handler and mail system, and my guess is that the entity is being magic-method serialized/toString'd. – mradcliffe Jan 11 '16 at 13:01
  • Not really, Message entity don't store any form fields. Message body is defined in contact_mail() function with this statement $message['body'][] = (string) \Drupal::service('renderer')->renderPlain($build); and before this statement I'm checking if field_customa and field_customb have any values. All other fields have their values, but the custom ones not. – Vagner Jan 11 '16 at 13:30
  • Yes, but the message body is comprised of parameters coming from MessageForm. – mradcliffe Jan 11 '16 at 13:35

Drupal 8 will handle custom fields on contact forms 'out-the-box', they will be rendered in the message body by contact module. If custom fields don't appear in message body, someone messed up.

In this particular case, attribute 'name' of 'input' html tags were modified, and $_POST came with a wrong schema. $_POST['field'] instead of $_POST['field'][0]['value'].

Here shows the hint:
"message":[{"value":"Any message"}] - Correct (field[0]['value'])
"field_customa":[] - Wrong (field[])

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.