You are here

function file_entity_field_formatter_view in File Entity (fieldable files) 7.3

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

Implements hook_field_formatter_view().

File

./file_entity.field.inc, line 351
Field API integration for the file_entity module.

Code

function file_entity_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $settings = $display['settings'];
  switch ($display['type']) {
    case 'file_rendered':
      foreach ($items as $delta => $item) {
        if (!empty($item)) {

          // The repeat-rendering protection may be disabled if necessary.
          if (variable_get('file_entity_protect_repeated_render', TRUE)) {

            // Protect ourselves from repeated rendering.
            static $repeated_render_depth = array();
            list($entity_id) = entity_extract_ids($entity_type, $entity);
            $repeated_render_id = $entity_type . $entity_id . $field['field_name'] . $item['fid'];
            if (isset($repeated_render_depth[$repeated_render_id])) {
              $repeated_render_depth[$repeated_render_id]++;
            }
            else {
              $repeated_render_depth[$repeated_render_id] = 1;
            }
            if ($repeated_render_depth[$repeated_render_id] > 20) {
              watchdog('file_entity', 'Repeated rendering detected when rendering entity %entity_type: %entity_id, using the %field_name field. Aborting rendering.', array(
                '%entity_type' => 'file',
                '%entity_id' => $item['fid'],
                '%field_name' => $field['field_name'],
              ), WATCHDOG_ERROR);
              return $element;
            }
          }
          $file = file_load($item['fid']);
          if (isset($item['display'])) {
            $file->display = $item['display'];
          }
          if (isset($item['description'])) {
            $file->description = $item['description'];
          }

          // 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'];

          // Prevent recursion by checking if the referencing entity is itself
          // do not display it again (return an empty array).
          // This senario is created a few lines below when file_view is called
          // it returns an array containing a referncing entity that is itself.
          // This is likely treating the symptom and not the root cause which is
          // probably buried in some hook_file_view() setting the referencing
          // entity.
          if (!empty($entity->fid) && $entity->fid === $item['fid']) {
            return $element;
          }

          // Untranslatable fields are rendered with no language code, fall back
          // to the content language in that case.
          $element[$delta] = file_view($file, $settings['file_view_mode'], $langcode !== LANGUAGE_NONE ? $langcode : NULL);
        }
        else {
          watchdog('file_entity', 'In the %referencing_entity_type, the %field_name field refers to a %entity_type which does not exist. Aborting the render for it.', array(
            '%referencing_entity_type' => $entity_type,
            '%field_name' => $field['field_name'],
            '%entity_type' => 'file',
          ), WATCHDOG_ERROR);
        }
      }
      break;
    case 'file_download_link':

      // Prevent 'empty' fields from causing a WSOD.
      $items = array_filter($items);
      foreach ($items as $delta => $item) {
        if (!empty($item['fid']) && ($file = file_load($item['fid'])) && file_entity_access('download', $file)) {
          if (isset($item['display'])) {
            $file->display = $item['display'];
          }
          if (isset($item['description'])) {
            $file->description = $item['description'];
          }
          $element[$delta] = array(
            '#theme' => 'file_entity_download_link',
            '#file' => $file,
            '#text' => $settings['text'],
          );
        }
      }
      break;
    case 'file_audio':
      $multiple_file_behavior = $settings['multiple_file_behavior'];

      // Build an array of sources for each <audio> element.
      $source_lists = array();
      if ($multiple_file_behavior == 'tags') {
        foreach ($items as $delta => $item) {
          if (file_entity_file_get_mimetype_type($item) == 'audio') {
            $source_lists[$delta] = array(
              $item,
            );
          }
        }
      }
      else {
        foreach ($items as $delta => $item) {
          if (file_entity_file_get_mimetype_type($item) == 'audio') {
            $source_lists[0][$delta] = $item;
          }
        }
      }

      // Render each source list as an <audio> element.
      foreach ($source_lists as $delta => $sources) {
        $element[$delta] = array(
          '#theme' => 'file_entity_file_audio',
          '#files' => $sources,
          '#controls' => $settings['controls'],
          '#controls_list' => $settings['controls_list'],
          '#autoplay' => $settings['autoplay'],
          '#loop' => $settings['loop'],
          '#preload' => $settings['preload'],
        );
      }
      break;
    case 'file_video':
      $multiple_file_behavior = $settings['multiple_file_behavior'];

      // Build an array of sources for each <video> element.
      $source_lists = array();
      if ($multiple_file_behavior == 'tags') {
        foreach ($items as $delta => $item) {
          if (file_entity_file_get_mimetype_type($item) == 'video') {
            $source_lists[$delta] = array(
              $item,
            );
          }
        }
      }
      else {
        foreach ($items as $delta => $item) {
          if (file_entity_file_get_mimetype_type($item) == 'video') {
            $source_lists[0][$delta] = $item;
          }
        }
      }

      // Render each source list as an <video> element.
      foreach ($source_lists as $delta => $sources) {
        $element[$delta] = array(
          '#theme' => 'file_entity_file_video',
          '#files' => $sources,
          '#controls' => $settings['controls'],
          '#controls_list' => $settings['controls_list'],
          '#autoplay' => $settings['autoplay'],
          '#playsinline' => $settings['playsinline'],
          '#loop' => $settings['loop'],
          '#muted' => $settings['muted'],
          '#width' => $settings['width'],
          '#height' => $settings['height'],
          '#preload' => $settings['preload'],
        );
      }
      break;
  }
  return $element;
}