diff --git a/file_entity.field.inc b/file_entity.field.inc index 5a82f23..9e22c5f 100644 --- a/file_entity.field.inc +++ b/file_entity.field.inc @@ -281,19 +281,24 @@ function file_entity_field_formatter_view($entity_type, $entity, $field, $instan switch ($display['type']) { case 'file_rendered': - $files = array(); - foreach ($items as $delta => $item) { - if (!empty($item['file'])) { - $files[$item['fid']] = $item['file']; + // Protect ourselves from recursive rendering. + static $depth = 0; + $depth++; + if ($depth > 20) { + throw new FileEntityRecursiveRenderingException(t('Recursive rendering detected when rendering entity @entity_type(@entity_id). Aborting rendering.', array('@entity_type' => 'file', '@entity_id' => $item['fid']))); } - } - if (!empty($files)) { - $output = file_view_multiple($files, $settings['file_view_mode'], 0, $langcode); + $file = (object) $item; - // Remove the first level from the output array. - $element = reset($output); + // Add some references to the referencing entity. + // @see https://www.drupal.org/node/2333107 + $file->referencing_entity = $entity; + $file->referencing_entity_type = $entity_type; + $file->referencing_field = $field['field_name']; + + $element[$delta] = file_view($file, $settings['file_view_mode'], $langcode); + $depth = 0; } break; @@ -388,3 +393,8 @@ function file_entity_field_formatter_view($entity_type, $entity, $field, $instan return $element; } + +/** + * Exception thrown when the file view renderer goes into a potentially infinite loop. + */ +class FileEntityRecursiveRenderingException extends Exception {}