How can I get the Contact Form "Manage Display" Settings programmatically? I am trying to use hook_mail_alter to create a custom contact mail.

I want to get the display setting of the contact form.

(example path: http://mysite/admin/structure/contact/manage/my_contact_form/display/mail)

The things I want to know are:


Field Label
If its disabled or not
Field order
Field ID


How can I get this information in the PHP script?

EDIT:

I was able to get most of the information I wanted. but I couldn't get the field weight to display in the right order. I have a display mode called 'mail' and I want to get the weight of each field in this mode.

for now the code looks like this:

/**
 * Implements hook_mail_alter().
 */
function contact_plus_mail_alter(&amp;$message) {
    if (isset($message['id']) &amp;&amp; $message['id'] == 'contact_page_mail') {

        $siteName = \Drupal::config('system.site')-&gt;get('name');
        $siteMail = \Drupal::config('system.site')-&gt;get('mail');
        /** @var \Drupal\contact\Entity\Message $contactMessage */
        $contactMessage = $message['params']['contact_message'];
        $entityType = $contactMessage-&gt;getEntityType()-&gt;id();
        $bundle = $contactMessage-&gt;bundle();

        $contactForm = $contactMessage-&gt;getContactForm();
        $form_title = $contactForm-&gt;label();
        $recipients = implode(',', $contactForm-&gt;getRecipients());
        /** @var \Drupal\Core\Field\FieldItemListInterface[] $fields */
        $fields = $contactMessage-&gt;getFields();
        $data = [
            'title' =&gt; $form_title,
            'fields' =&gt; [],
        ];
        foreach ($fields as $key =&gt; $field) {
            $fieldDefinition = $field-&gt;getFieldDefinition();
            $data['fields'][] = [
                'label' =&gt; $fieldDefinition-&gt;getLabel(),
                'info' =&gt; $field-&gt;getValue(),
                'display_options' =&gt; $fieldDefinition-&gt;getDisplayOptions(),
                'weight' =&gt; '', // I want to get the field weight for the 'mail' display mode.
            ];
        }
        // TODO: order the data by weight

        // ... send the email
    }
}
I was able to retrieve the order of the 'mail' display.
The secret was in the -&gt;getComponets() function in this part:

$display = \Drupal::entityTypeManager()
    -&gt;getStorage('entity_view_display')
    -&gt;load($entityType . '.' . $bundle . '.' . $displayMode);
$displayFields = $display-&gt;getComponents();


the final code looks like this:

/**
 * Implements hook_mail_alter().
 */
function contact_plus_mail_alter(&amp;$message) {
    if (isset($message['id']) &amp;&amp; $message['id'] == 'contact_page_mail') {
        $message['send'] = false;

        $siteName = \Drupal::config('system.site')-&gt;get('name');
        $siteMail = \Drupal::config('system.site')-&gt;get('mail');

        /** @var \Drupal\contact\Entity\Message $contactMessage */
        $contactMessage = $message['params']['contact_message'];
        $entityType = $contactMessage-&gt;getEntityType()-&gt;id();
        $bundle = $contactMessage-&gt;bundle();
        $contactForm = $contactMessage-&gt;getContactForm();
        $form_title = $contactForm-&gt;label();
        $recipients = implode(',', $contactForm-&gt;getRecipients());
        // Data for the email
        $data = [
            'title' =&gt; $form_title,
            'today' =&gt; (new \DateTime('now', new \DateTimeZone('UTC')))-&gt;format('m/d/Y'),
            'fields' =&gt; [],
        ];
        // get the 'mail' display mode if defined
        $viewModes = \Drupal::service('entity_display.repository')
            -&gt;getViewModeOptionsByBundle(
                $entityType, $bundle
            );
        $displayMode = array_key_exists('mail', $viewModes) ? 'mail' : 'default';

        // get display field settings (weight, id, etc...)
        /** @var \Drupal\Core\Entity\Entity\EntityViewDisplay $display */
        $display = \Drupal::entityTypeManager()
            -&gt;getStorage('entity_view_display')
            -&gt;load($entityType . '.' . $bundle . '.' . $displayMode);
        $displayFields = $display-&gt;getComponents();

        // put the fields info in the email data
        foreach ($displayFields as $key =&gt; $fieldConfig) {
            $field = $contactMessage-&gt;get($key);
            $fieldDefinition = $field-&gt;getFieldDefinition();
            $data['fields'][$key] = [
                'label' =&gt; $fieldDefinition-&gt;getLabel(),
                'info' =&gt; $field-&gt;getValue(),
                'display_options' =&gt; $fieldDefinition-&gt;getDisplayOptions(),
                'weight' =&gt; $fieldConfig['weight'],
            ];
        }

        // order by weight
        function compare_weight($a, $b) {
            return strnatcmp($a['weight'], $b['weight']);
        }
        uasort($data['fields'], 'compare_weight');

        // send the email for the responsible
        $emailSender = new EmailSender($siteName . '&lt;' . $siteMail . '&gt;', $recipients, $form_title, 'my_theme', $data);
        $emailSender-&gt;createAndSendEmail();

        // send a copy to the person who filed the form
        if (isset($data['fields']['field_email'])) {
            $email = $data['fields']['field_email']['info'][0]['value'];
            $emailSender-&gt;to = $email;
        }
        $emailSender-&gt;createAndSendEmail();
    }
}


Hope it helps someone.