You are here

public function EmailField::prepareRecipients in Workbench Email 8

Same name and namespace in other branches
  1. 2.x src/Plugin/RecipientType/EmailField.php \Drupal\workbench_email\Plugin\RecipientType\EmailField::prepareRecipients()

Returns email address(s) matching this recipient type's configuration.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: Entity being transitioned.

\Drupal\workbench_email\TemplateInterface $template: Template being used.

Overrides RecipientTypeBase::prepareRecipients

File

src/Plugin/RecipientType/EmailField.php, line 139

Class

EmailField
Provides a recipient type of an email field.

Namespace

Drupal\workbench_email\Plugin\RecipientType

Code

public function prepareRecipients(ContentEntityInterface $entity, TemplateInterface $template) {
  $recipients = [];
  $fields = array_filter($this
    ->getFields(), function ($field_name) use ($entity) {
    list($entity_type, $field_name) = explode(':', $field_name, 2);
    return $entity_type === $entity
      ->getEntityTypeId() && $entity
      ->hasField($field_name) && !$entity->{$field_name}
      ->isEmpty();
  });
  foreach ($fields as $field) {
    list(, $field_name) = explode(':', $field, 2);

    /** @var \Drupal\Core\Field\FieldItemInterface $field_item */
    foreach ($entity->{$field_name} as $field_item) {
      $recipients[] = $this
        ->getEmailFromFieldItem($field_item);
    }
  }
  return $recipients;
}