1.20.x file.field.inc file_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display)

Implements hook_field_formatter_view().

File

modules/file/file.field.inc, line 1001
Field module functionality for the File module.

Code

function file_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $settings = isset($display['settings']) ? $display['settings'] : array();

  switch ($display['type']) {
    case 'file_default':
      foreach ($items as $delta => $item) {
        $element[$delta] = array(
          '#theme' => 'file_link',
          '#file' => (object) $item,
        );
      }
      break;

    case 'file_url_plain':
      foreach ($items as $delta => $item) {
        $element[$delta] = array('#markup' => empty($item['uri']) ? '' : file_create_url($item['uri']));
      }
      break;

    case 'file_table':
      if (!empty($items)) {
        // Display all values in a single element..
        $element[0] = array(
          '#theme' => 'file_formatter_table',
          '#items' => $items,
        );
      }
      break;

    case 'file_rendered':
      foreach ($items as $delta => $item) {
        // Protect ourselves from recursive rendering.
        static $recursive_render_depth = array();
        if (!empty($item)) {
          // The repeat-rendering protection may be disabled if necessary.
          if (config_get('file.sttings', 'protect_repeated_render')) {
            // Protect ourselves from repeated rendering.
            static $repeated_render_depth = array();
            $repeated_render_id = $entity_type . $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] > 10) {
              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'];

          // 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 {
          $variables = array(
            '%referencing_entity_type' => $entity_type,
            '%field_name' => $field['field_name'],
            '%entity_type' => 'file',
          );
          $message = 'In the %referencing_entity_type, the %field_name field refers to a %entity_type which does not exist. Aborting the render for it.';
          watchdog('file', $message, $variables, 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_access('download', $file)) {
          $element[$delta] = array(
            '#theme' => 'file_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_get_mimetype_type($item) == 'audio') {
            $source_lists[$delta] = array($item);
          }
        }
      }
      else {
        foreach ($items as $delta => $item) {
          if (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_audio',
          '#files' => $sources,
          '#controls' => $settings['controls'],
          '#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_get_mimetype_type($item) == 'video') {
            $source_lists[$delta] = array($item);
          }
        }
      }
      else {
        foreach ($items as $delta => $item) {
          if (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_video',
          '#files' => $sources,
          '#controls' => $settings['controls'],
          '#autoplay' => $settings['autoplay'],
          '#loop' => $settings['loop'],
          '#muted' => $settings['muted'],
          '#width' => $settings['width'],
          '#height' => $settings['height'],
          '#preload' => $settings['preload'],
        );
      }
      break;

  }

  return $element;
}