You are here

function file_entity_file_formatter_file_field_view in File Entity (fieldable files) 7

Same name and namespace in other branches
  1. 7.3 file_entity.module \file_entity_file_formatter_file_field_view()
  2. 7.2 file_entity.module \file_entity_file_formatter_file_field_view()

Implements hook_file_formatter_FORMATTER_view().

This function provides a bridge to the field formatter API, so that file field formatters can be reused for displaying the file entity's file pseudo-field.

1 string reference to 'file_entity_file_formatter_file_field_view'
file_entity_file_formatter_info in ./file_entity.module
Implements hook_file_formatter_info().

File

./file_entity.module, line 348
Extends Drupal file entities to be fieldable and viewable.

Code

function file_entity_file_formatter_file_field_view($file, $display, $langcode) {
  if (strpos($display['type'], 'file_field_') === 0) {
    $field_formatter_type = substr($display['type'], strlen('file_field_'));
    $field_formatter_info = field_info_formatter_types($field_formatter_type);
    if (isset($field_formatter_info['module'])) {

      // Set $display['type'] to what hook_field_formatter_*() expects.
      $display['type'] = $field_formatter_type;

      // Set $items to what file field formatters expect. See file_field_load(),
      // and note that, here, $file is already a fully loaded entity.
      $items = array(
        (array) $file,
      );

      // Invoke hook_field_formatter_prepare_view() and
      // hook_field_formatter_view(). Note that we are reusing field formatter
      // functions, but we are not displaying a Field API field, so we set
      // $field and $instance accordingly, and do not invoke
      // hook_field_prepare_view(). This assumes that the formatter functions do
      // not rely on $field or $instance. A module that implements formatter
      // functions that rely on $field or $instance (and therefore, can only be
      // used for real fields) can prevent this formatter from being used on the
      // pseudo-field by removing it within hook_file_formatter_info_alter().
      $field = $instance = NULL;
      if (($function = $field_formatter_info['module'] . '_field_formatter_prepare_view') && function_exists($function)) {
        $fid = $file->fid;

        // hook_field_formatter_prepare_view() alters $items by reference.
        $grouped_items = array(
          $fid => &$items,
        );
        $function('file', array(
          $fid => $file,
        ), $field, array(
          $fid => $instance,
        ), $langcode, $grouped_items, array(
          $fid => $display,
        ));
      }
      if (($function = $field_formatter_info['module'] . '_field_formatter_view') && function_exists($function)) {
        $element = $function('file', $file, $field, $instance, $langcode, $items, $display);

        // We passed the file as $items[0], so return the corresponding element.
        if (isset($element[0])) {
          return $element[0];
        }
      }
    }
  }
}