0

I'm trying to pass Drupal 8 core Contact form data to a custom designed Swift Mailer template. The way I'm trying to do this is by getting all the fields dynamically to the template.

function MYMODULE_preprocess_swiftmailer(&$variables) {

  if (isset($variables['message']['params']['contact_message'])) {

    $fields = $variables['message']['params']['contact_message']->getFields();

    $i = 0;
    foreach ($fields as $key => $field) {

      //every field with field_ prefix exept email & message
      if (strpos($key, 'field_') !== FALSE && strpos($key, '_message') === FALSE && strpos($key, 'bericht') === FALSE) {

        $variables['fields'][$i]['name'] = $field->getFieldDefinition()->getLabel();
        $variables['fields'][$i]['value'] = $field->getValue()[0]['value'];
        $i++;
      }
      elseif (strpos($key, '_message') !== FALSE || strpos($key, 'bericht') !== FALSE) {

        $variables['message']['name'] = $field->getFieldDefinition()->getLabel();
        $variables['message']['value'] = $field->getValue()[0]['value'];
      }
      elseif (strpos($key, 'mail') !== FALSE) {

        $variables['fields'][$i]['name'] = t('E-mail');
        $variables['fields'][$i]['value'] = $field->getValue()[0]['value'];
        $i++;
      }
    }
  }
}

This works for all basic (text) fields, but not for input type files. Also the order/sequence of the fields is not the same as set in /admin/structure/contact/manage/feedback/display.

I want to display data this way in the template:

{% for field in fields %}
  <td>{{ field.name }}</td>
  <td>{{ field.value }}</td>
{% endfor %}

So the input type="file" should be displayed as an url to the attachment. Now I'm getting NULL. enter image description here

Is there maybe a better / easier way to do this in Drupal 8? Or a tutorial the achieve the same goal?

Thanks in advance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.