You are here

function commerce_file_field_formatter_view in Commerce File 7.2

Same name and namespace in other branches
  1. 7 commerce_file.module \commerce_file_field_formatter_view()

Implements hook_field_formatter_view().

File

./commerce_file.module, line 822
Extends Commerce License with the ability to sell access to files.

Code

function commerce_file_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $settings = $display['settings'];
  $element = array();
  if ($display['type'] == 'commerce_file') {

    // This formatter only works on commerce_file fields attached to products.
    if ($entity_type != 'commerce_product') {
      return $element;
    }
    $license = commerce_file_get_product_license($entity);
    $access = TRUE;
    if (!empty($settings['check_access'])) {
      $access = $license || commerce_file_bypass_license_control();
    }
    foreach ($items as $delta => $item) {
      $file = (object) $item;
      $icon = NULL;
      if (!empty($settings['show_icon'])) {
        $icon = theme('file_icon', array(
          'file' => $file,
        ));
      }
      $filesize = NULL;
      if (!empty($settings['show_filesize'])) {
        $filesize = format_size($file->filesize);
      }
      if ($access) {
        $element[$delta] = array(
          '#theme' => 'commerce_file_download_link',
          '#file' => $file,
          '#filesize' => $filesize,
          '#license' => $license,
          '#icon' => $icon,
        );
      }
    }
  }
  return $element;
}