You are here

public function MailEntity::getAttachments in Simplenews 8.2

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

Returns an array of attachments for this newsletter mail.

Return value

array An array of managed file objects with properties uri, filemime and so on.

Overrides MailInterface::getAttachments

File

src/Mail/MailEntity.php, line 389

Class

MailEntity
Default mail class for entities.

Namespace

Drupal\simplenews\Mail

Code

public function getAttachments() {
  if ($cache = $this->cache
    ->get($this, 'data', 'attachments')) {
    return $cache;
  }
  $attachments = [];
  $build = $this
    ->build();
  $fids = [];
  foreach ($this
    ->getIssue()
    ->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
        ->getIssue()
        ->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;
}