diff --git a/file_entity.field.inc b/file_entity.field.inc index 5a82f23..dc32dc9 100644 --- a/file_entity.field.inc +++ b/file_entity.field.inc @@ -281,19 +281,36 @@ 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) { + watchdog( + 'file_entity', + 'Recursive rendering detected when rendering entity %entity_type: %entity_id, using the %field_name field on the %bundle_name bundle. Aborting rendering.', + array( + '%entity_type' => 'file', + '%entity_id' => $item['fid'], + '%field_name' => $field['field_name'], + '%bundle_name' => $instance['bundle'], + ), + WATCHDOG_ERROR + ); + return $element; } - } - 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;