You are here

function file_displays in File Entity (fieldable files) 7.3

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

Returns an array of displays to use for a file type in a given view mode.

It is common for a site to be configured with broadly defined file types (e.g., 'video'), and to have different files of this type require different displays (for example, the code required to display a YouTube video is different than the code required to display a local QuickTime video). Therefore, the site administrator can configure multiple displays for a given file type. This function returns all of the displays that the administrator enabled for the given file type in the given view mode. file_view_file() then invokes each of these, and passes the specific file to display. Each display implementation can inspect the file, and either return a render array (if it is capable of displaying the file), or return nothing (if it is incapable of displaying the file). The first render array returned is the one used.

Parameters

string $file_type: The type of file.

string $view_mode: The view mode.

Return value

array An array keyed by the formatter type name. Each item in the array contains the following key/value pairs:

  • status: Whether this display is enabled. If not TRUE, file_view_file() skips over it.
  • weight: An integer that determines the order of precedence within the returned array. The lowest weight display capable of displaying the file is used.
  • settings: An array of key/value pairs specific to the formatter type. See hook_file_formatter_info() for details.

See also

hook_file_formatter_info()

file_view_file()

Related topics

1 call to file_displays()
file_view_file in ./file_entity.file_api.inc
Generate an array for rendering just the file portion of a file entity.
1 string reference to 'file_displays'
file_entity_update_7001 in ./file_entity.install
Move file display configurations.

File

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

Code

function file_displays($file_type, $view_mode) {
  $cache =& drupal_static(__FUNCTION__, array());

  // If the requested view mode isn't configured to use a custom display for its
  // fields, then don't use a custom display for its file either.
  if ($view_mode != 'default') {
    $view_mode_settings = field_view_mode_settings('file', $file_type);
    $view_mode = !empty($view_mode_settings[$view_mode]['custom_settings']) ? $view_mode : 'default';
  }
  if (!isset($cache[$file_type][$view_mode])) {

    // Load the display configurations for the file type and view mode. If none
    // exist for the view mode, use the default view mode.
    $displays = file_displays_load($file_type, $view_mode, TRUE);
    if (empty($displays) && $view_mode != 'default') {
      $cache[$file_type][$view_mode] = file_displays($file_type, 'default');
    }
    else {

      // Convert the display objects to arrays and remove unnecessary keys.
      foreach ($displays as $formatter_name => $display) {
        $displays[$formatter_name] = array_intersect_key((array) $display, drupal_map_assoc(array(
          'status',
          'weight',
          'settings',
        )));
      }
      $cache[$file_type][$view_mode] = $displays;
    }
  }
  return $cache[$file_type][$view_mode];
}