function email_contact_get_emails_from_field in Email Contact 8
Return emails from entity and given field name.
Parameters
string $entity_type: The type of requested entity.
string $id: The id of the requested entity.
object $field_name: The name of the email field.
Return value
array An array with valid email addresses.
1 call to email_contact_get_emails_from_field()
- ContactForm::buildForm in src/
Form/ ContactForm.php - Form constructor.
File
- ./
email_contact.module, line 25 - Email Contact module file.
Code
function email_contact_get_emails_from_field($entity_type, $id, $field_name) {
if (!is_numeric($id)) {
throw new NotFoundHttpException();
}
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = \Drupal::entityTypeManager()
->getStorage($entity_type)
->load($id);
// Check that the entity exists.
if ($entity === NULL) {
throw new NotFoundHttpException();
}
// Check that the field exists and has the right type.
if (!$entity
->hasField($field_name)) {
throw new NotFoundHttpException();
}
$field = $entity
->get($field_name);
if ($field === NULL || $field
->getFieldDefinition()
->getType() != 'email') {
throw new NotFoundHttpException();
}
// Load email addresses
foreach ($field
->getIterator() as $item) {
$value = $item
->getValue()['value'];
if (!empty($value) && strpos($value, '@') !== FALSE) {
$emails[] = $value;
}
}
// Verify that the email address is not empty.
if (empty($emails)) {
throw new NotFoundHttpException();
}
return $emails;
}