You are here

function file_entity_file_formatter_file_image_view in File Entity (fieldable files) 7.2

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

Implements hook_file_formatter_FORMATTER_view().

Returns a drupal_render() array to display an image of the chosen style.

This formatter is only capable of displaying local images. If the passed in file is either not local or not an image, nothing is returned, so that file_view_file() can try another formatter.

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

File

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

Code

function file_entity_file_formatter_file_image_view($file, $display, $langcode) {

  // Prevent PHP notices when trying to read empty files.
  // @see http://drupal.org/node/681042
  if (!$file->filesize) {
    return;
  }

  // Do not bother proceeding if this file does not have an image mime type.
  if (file_entity_file_get_mimetype_type($file) != 'image') {
    return;
  }
  if (file_entity_file_is_readable($file)) {

    // We don't sanitize here.
    // @see http://drupal.org/node/1553094#comment-6257382
    // Theme function will take care of escaping.
    if (!isset($file->metadata)) {
      $file->metadata = array();
    }
    $file->metadata += array(
      'width' => NULL,
      'height' => NULL,
    );
    $replace_options = array(
      'clear' => TRUE,
      'sanitize' => FALSE,
    );
    if (!empty($display['settings']['image_style'])) {
      $element = array(
        '#theme' => 'image_style',
        '#style_name' => $display['settings']['image_style'],
        '#path' => $file->uri,
        '#width' => isset($file->override['attributes']['width']) ? $file->override['attributes']['width'] : $file->metadata['width'],
        '#height' => isset($file->override['attributes']['height']) ? $file->override['attributes']['height'] : $file->metadata['height'],
        '#alt' => file_entity_replace_alt($file, $replace_options, $display['settings']['alt'], $langcode),
        '#title' => file_entity_replace_title($file, $replace_options, $display['settings']['title'], $langcode),
      );
    }
    else {
      $element = array(
        '#theme' => 'image',
        '#path' => $file->uri,
        '#width' => isset($file->override['attributes']['width']) ? $file->override['attributes']['width'] : $file->metadata['width'],
        '#height' => isset($file->override['attributes']['height']) ? $file->override['attributes']['height'] : $file->metadata['height'],
        '#alt' => file_entity_replace_alt($file, $replace_options, $display['settings']['alt'], $langcode),
        '#title' => file_entity_replace_title($file, $replace_options, $display['settings']['title'], $langcode),
      );
    }
    return $element;
  }
}