You are here

node_field_file.formatter.inc in Node Field 7.2

Formatter functions for node_field_file module.

File

modules/node_field_file/includes/node_field_file.formatter.inc
View source
<?php

/**
 * @file
 * Formatter functions for node_field_file module.
 */

/**
 * File formatters.
 */
function node_field_file_formatter_info() {
  $items = array();
  $items['node_field_file_link'] = array(
    '#title' => t('Link'),
    '#formatter' => 'node_field_file_link_formatter',
  );
  $items['node_field_file_image'] = array(
    '#title' => t('Image'),
    '#formatter' => 'node_field_file_image_formatter',
  );
  $items['node_field_file_audio'] = array(
    '#title' => t('Audio'),
    '#formatter' => 'node_field_file_audio_formatter',
  );
  $items['node_field_file_video'] = array(
    '#title' => t('Video'),
    '#formatter' => 'node_field_file_video_formatter',
  );
  return $items;
}

/**
 * Format file field as link to file.
 *
 * @param array $field
 *   Field to get formatted output for.
 */
function node_field_file_link_formatter(array $field) {
  $output = '';
  if (!empty($field['value'])) {
    $file = file_load($field['value']);
    if (!empty($file)) {
      $url = file_create_url($file->uri);
      $target = empty($field['settings']['link_target']) ? '_blank' : $field['settings']['link_target'];
      $options = array(
        'attributes' => array(
          'target' => $target,
          'class' => $target == '_blank' ? array(
            'new-window',
          ) : array(),
        ),
      );
      $output = l($file->filename, $url, $options);
    }
  }
  return $output;
}

/**
 * Format file field as image.
 *
 * @param array $field
 *   Field to get formatted output for.
 */
function node_field_file_image_formatter(array $field) {
  $output = '';
  if (!empty($field['value'])) {
    $file = file_load($field['value']);
    $url = file_create_url($file->uri);
    if (empty($field['settings']['image_style']) || $field['settings']['image_style'] == 'original') {
      $output = theme('image', array(
        'path' => $url,
      ));
    }
    else {
      $output = theme('image_style', array(
        'style_name' => $field['settings']['image_style'],
        'path' => $file->uri,
      ));
    }
  }
  return $output;
}

/**
 * Format file field as audio element.
 *
 * @param array $field
 *   Field to get formatted output for.
 */
function node_field_file_audio_formatter(array $field) {
  $output = '';
  if (!empty($field['value'])) {
    $fid = intval($field['value']);
    $file = file_load($fid);
    $url = file_create_url($file->uri);
    $width = NODE_FIELD_FILE_DEFAULT_WIDTH;
    if (!empty($field['settings']['size']['width'])) {
      $width = $field['settings']['size']['width'];
    }
    $height = NODE_FIELD_FILE_DEFAULT_HEIGHT;
    if (!empty($field['settings']['size']['height'])) {
      $height = $field['settings']['size']['height'];
    }
    $element = array(
      '#type' => 'html_tag',
      '#tag' => 'audio',
      '#attributes' => array(
        'width' => $width,
        'height' => $height,
        'id' => 'node_field_file_audio_' . $fid,
        'src' => $url,
      ),
    );
    $output = drupal_render($element);
  }
  return $output;
}

/**
 * Format file field as video element.
 *
 * @param array $field
 *   Field to get formatted output for.
 */
function node_field_file_video_formatter(array $field) {
  $output = '';
  if (!empty($field['value'])) {
    $fid = intval($field['value']);
    $file = file_load($fid);
    $url = file_create_url($file->uri);
    $width = NODE_FIELD_FILE_DEFAULT_WIDTH;
    if (!empty($field['settings']['size']['width'])) {
      $width = $field['settings']['size']['width'];
    }
    $height = NODE_FIELD_FILE_DEFAULT_HEIGHT;
    if (!empty($field['settings']['size']['height'])) {
      $height = $field['settings']['size']['height'];
    }
    $element = array(
      '#type' => 'html_tag',
      '#tag' => 'video',
      '#attributes' => array(
        'width' => $width,
        'height' => $height,
        'id' => 'node_field_file_video_' . $fid,
        'src' => $url,
      ),
    );
    $output = drupal_render($element);
  }
  return $output;
}

Functions

Namesort descending Description
node_field_file_audio_formatter Format file field as audio element.
node_field_file_formatter_info File formatters.
node_field_file_image_formatter Format file field as image.
node_field_file_link_formatter Format file field as link to file.
node_field_file_video_formatter Format file field as video element.