You are here

public function ContactEmails::getContactFormFields in Contact Emails 8

Get contact form fields by type.

Parameters

string $contact_form_id: The ID of the contact form.

string $field_type: The type of field to get.

Return value

mixed The array of email field keys.

File

src/ContactEmails.php, line 120

Class

ContactEmails
Class ContactEmails.

Namespace

Drupal\contact_emails

Code

public function getContactFormFields($contact_form_id, $field_type) {
  $available_fields = [];

  // Get all field entities attached to the particular contact form.
  $fields = array_filter($this->entityFieldManager
    ->getFieldDefinitions('contact_message', $contact_form_id), function ($field_definition) {
    return $field_definition instanceof FieldConfigInterface;
  });
  if ($fields) {

    /** @var \Drupal\Core\Field\FieldDefinitionInterface $field */
    foreach ($fields as $field) {
      $type = $field
        ->getType();
      if ($type == $field_type) {
        switch ($type) {
          case 'email':
            $available_fields = $this
              ->getEmailField($available_fields, $field);
            break;
          case 'entity_reference':
            $available_fields = $this
              ->getEntityReferenceEmailFields($available_fields, $field);
            break;
        }
      }
    }
  }
  return $available_fields;
}