I have a quite complex form based on \Drupal\Core\Form\FormBase
with many (partially nested) fields and want to send the full contents in an email on submit.
Currently I have
public function submitForm(array &$form, FormStateInterface $form_state)
{
foreach ($form_state->getValues() as $key => $value) {
if (!empty($form[$key]['#title']) && $key != 'images') {
$values[$form[$key]['#title']->render()] = $value;
}
}
...
# later I will format the $values array as a HTML list
This would only show fields on the top-level of the form, but I have fields within groups.
Is there a nice and clean way to render all the field's values? Or do I have to iterate and parse manually over the $form
array?
Could I even use a renderer for this?
EDIT:
public function buildForm(array $form, FormStateInterface $form_state) {
// ...
$form['company'] = [
'#type' => 'container',
];
$form['company']['nameofcompany'] = [
'#type' => 'textfield',
'#title' => $this->t('Name of Company'),
];
To sum it up:
How can I iterate over all the fields, including those which are in containers and get the values + the label (not only the technical name)? Are there some nice helpers for this or do I have to do it all manually?