You are here

function file_build_content in File Entity (fieldable files) 7.2

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

Builds a structured array representing the file's content.

Parameters

object $file: A file object.

string $view_mode: View mode, e.g. 'default', 'full', etc.

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

2 calls to file_build_content()
file_entity_file_content_content_type_render in plugins/content_types/file_content.inc
Render the node content.
file_view in ./file_entity.file_api.inc
Generate an array for rendering the given file.

File

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

Code

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

  // Remove previously built content, if exists.
  $file->content = array();

  // In case of a multiple view, file_view_multiple() already ran the
  // 'prepare_view' step. An internal flag prevents the operation from running
  // twice.
  // Allow modules to change the view mode.
  $view_mode = key(entity_view_mode_prepare('file', array(
    $file->fid => $file,
  ), $view_mode, $langcode));
  field_attach_prepare_view('file', array(
    $file->fid => $file,
  ), $view_mode, $langcode);
  entity_prepare_view('file', array(
    $file->fid => $file,
  ), $langcode);

  // Build the actual file display.
  // @todo Figure out how to clean this crap up.
  $file->content['file'] = file_view_file($file, $view_mode, $langcode);
  if (isset($file->content['file'])) {
    if (isset($file->content['file']['#theme']) && $file->content['file']['#theme'] != 'file_link') {
      unset($file->content['file']['#file']);
    }
    unset($file->content['file']['#view_mode']);
    unset($file->content['file']['#language']);
  }
  else {
    unset($file->content['file']);
  }

  // Build fields content.
  $file->content += field_attach_view('file', $file, $view_mode, $langcode);
  $links = array();
  $file->content['links'] = array(
    '#theme' => 'links__file',
    '#pre_render' => array(
      'drupal_pre_render_links',
    ),
    '#attributes' => array(
      'class' => array(
        'links',
        'inline',
      ),
    ),
  );
  $file->content['links']['file'] = array(
    '#theme' => 'links__file__file',
    '#links' => $links,
    '#attributes' => array(
      'class' => array(
        'links',
        'inline',
      ),
    ),
  );

  // Allow modules to make their own additions to the file.
  module_invoke_all('file_view', $file, $view_mode, $langcode);
  module_invoke_all('entity_view', $file, 'file', $view_mode, $langcode);
}