You are here

function MailEntity::getAttachments in Simplenews 8

Same name and namespace in other branches
  1. 8.2 src/Mail/MailEntity.php \Drupal\simplenews\Mail\MailEntity::getAttachments()
  2. 3.x src/Mail/MailEntity.php \Drupal\simplenews\Mail\MailEntity::getAttachments()

{@inhertidoc}

Overrides MailInterface::getAttachments

File

src/Mail/MailEntity.php, line 406

Class

MailEntity
Default mail class for entities.

Namespace

Drupal\simplenews\Mail

Code

function getAttachments() {
  if ($cache = $this->cache
    ->get($this, 'data', 'attachments')) {
    return $cache;
  }
  $attachments = array();
  $build = $this
    ->build();
  $fids = array();
  foreach ($this
    ->getEntity()
    ->getFieldDefinitions() as $field_name => $field_definition) {

    // @todo: Find a better way to support more field types.
    // Only add fields of type file which are enabled for the current view
    // mode as attachments.
    if ($field_definition
      ->getType() == 'file' && isset($build[$field_name])) {
      if ($items = $this
        ->getEntity()
        ->get($field_name)) {
        foreach ($items as $item) {
          $fids[] = $item->target_id;
        }
      }
    }
  }
  if (!empty($fids)) {
    $attachments = File::loadMultiple($fids);
  }
  $this->cache
    ->set($this, 'data', 'attachments', $attachments);
  return $attachments;
}