You are here

function SimplenewsSourceEntity::getAttachments in Simplenews 7.2

Implements SimplenewsSourceInterface::getAttachments().

Overrides SimplenewsSourceInterface::getAttachments

File

includes/simplenews.source.inc, line 794
Contains SimplenewsSource interface and implementations.

Class

SimplenewsSourceEntity
Default source class for entities.

Code

function getAttachments() {
  if ($cache = $this->cache
    ->get('data', 'attachments')) {
    return $cache;
  }
  $attachments = array();
  $build = $this
    ->build();
  $fids = array();
  list(, , $bundle) = entity_extract_ids($this
    ->getEntityType(), $this
    ->getEntity());
  foreach (field_info_instances($this
    ->getEntityType(), $bundle) as $field_name => $field_instance) {

    // @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.
    $field = field_info_field($field_name);
    if ($field['type'] == 'file' && isset($build[$field_name])) {
      if ($items = field_get_items('node', $this->node, $field_name)) {
        foreach ($items as $item) {
          $fids[] = $item['fid'];
        }
      }
    }
  }
  if (!empty($fids)) {
    $attachments = file_load_multiple($fids);
  }
  $this->cache
    ->set('data', 'attachments', $attachments);
  return $attachments;
}