You are here

function file_view_file in File Entity (fieldable files) 7.2

Same name and namespace in other branches
  1. 7.3 file_entity.file_api.inc \file_view_file()
  2. 7 file_entity.file_api.inc \file_view_file()

Generate an array for rendering just the file portion of a file entity.

Parameters

object $file: A file object.

string|array $displays: Can be either:

  • the name of a view mode;
  • or an array of custom display settings, as returned by file_displays().

string $langcode: (optional) A language code to use for rendering. Defaults to the global content language of the current request.

Return value

array An array as expected by drupal_render().

5 calls to file_view_file()
FileEntityAttributeOverrideTestCase::testFileEntityFileAttributeOverrides in ./file_entity.test
Test to see if file attributes can be overridden.
file_build_content in ./file_entity.file_api.inc
Builds a structured array representing the file's content.
file_entity_edit in ./file_entity.pages.inc
Page callback: Form constructor for the file edit form.
file_entity_file_display_content_type_render in plugins/content_types/file_display.inc
Render the node content.
views_handler_field_file_rendered::render in views/views_handler_field_file_rendered.inc
Render the field.

File

./file_entity.file_api.inc, line 231
API extensions of Drupal core's file.inc.

Code

function file_view_file($file, $displays = 'full', $langcode = NULL) {
  if (!isset($langcode)) {
    $langcode = $GLOBALS['language_content']->language;
  }

  // Prepare incoming display specifications.
  if (is_string($displays)) {

    // Allow modules to change the view mode.
    $view_mode = key(entity_view_mode_prepare('file', array(
      $file->fid => $file,
    ), $displays, $langcode));
    $displays = file_displays($file->type, $view_mode);
  }
  else {
    $view_mode = '_custom_display';
  }
  drupal_alter('file_displays', $displays, $file, $view_mode);
  _file_sort_array_by_weight($displays);

  // Since $file->alt and $file->title were set in file_entity_file_load()
  // (which is a language-agnostic hook) they will not be in the correct
  // language if the file is being displayed in a language other than the
  // default one. Set them again here, using the correct language. This must
  // run after hook_file_displays_alter() since the Media module sets
  // $file->alt and $file->title again during that hook.
  if ($langcode != $GLOBALS['language_content']->language) {
    $languages = language_list();
    if (isset($languages[$langcode])) {
      if (!function_exists('file_entity_set_title_alt_properties')) {
        module_load_include('file.inc', 'file_entity');
      }
      file_entity_set_title_alt_properties(array(
        $file,
      ), $languages[$langcode]);
    }
  }

  // Attempt to display the file with each of the possible displays. Stop after
  // the first successful one. See file_displays() for details.
  $element = NULL;
  foreach ($displays as $formatter_type => $display) {
    if (!empty($display['status'])) {
      $formatter_info = file_info_formatter_types($formatter_type);

      // Under normal circumstances, the UI prevents enabling formatters for
      // incompatible MIME types. In case this was somehow circumvented (for
      // example, a module updated its formatter definition without updating
      // existing display settings), perform an extra check here.
      if (isset($formatter_info['mime types'])) {
        if (!file_entity_match_mimetypes($formatter_info['mime types'], $file->filemime)) {
          continue;
        }
      }
      if (isset($formatter_info['view callback']) && ($function = $formatter_info['view callback']) && function_exists($function)) {
        $display['type'] = $formatter_type;
        if (!empty($formatter_info['default settings'])) {
          if (empty($display['settings'])) {
            $display['settings'] = array();
          }
          $display['settings'] += $formatter_info['default settings'];
        }
        $element = $function($file, $display, $langcode);
        if (isset($element)) {
          break;
        }
      }
    }
  }

  // As a last resort, fall back to showing a link to the file.
  if (!isset($element)) {
    $element = array(
      '#theme' => 'file_link',
      '#file' => $file,
    );
  }

  // Add defaults and return the element.
  $element += array(
    '#file' => $file,
    '#view_mode' => $view_mode,
    '#language' => $langcode,
  );
  return $element;
}