You are here

function asset_asset_formatter in Asset 6

Same name and namespace in other branches
  1. 5.2 asset.module \asset_asset_formatter()
  2. 5 asset.module \asset_asset_formatter()

Implementation of hook_asset_formatter().

File

inc/asset.api.inc, line 5

Code

function asset_asset_formatter($op = 'info', $asset = NULL, $attr = array()) {
  switch ($op) {
    case 'info':
      $formats['link'] = array(
        'name' => t('Link'),
        'types' => array(
          '*' => array(
            '*',
          ),
        ),
        'description' => t('A simple download link.'),
      );
      $formats['image'] = array(
        'name' => t('Image'),
        'types' => array(
          'local' => array(
            'jpg',
            'gif',
            'png',
          ),
        ),
        'description' => t('The full-size image.'),
      );
      return $formats;
    case 'options':
      switch ($attr['format']) {
        case 'image':
          $info = image_get_info(file_create_path($asset->filepath));
          $form['height'] = array(
            '#type' => 'textfield',
            '#title' => t('Height'),
            '#size' => '10',
            '#default_value' => !isset($_GET['height']) ? $info['height'] : filter_xss($_GET['height']),
          );
          $form['width'] = array(
            '#type' => 'textfield',
            '#title' => t('Width'),
            '#size' => '10',
            '#default_value' => !isset($_GET['width']) ? $info['width'] : filter_xss($_GET['width']),
          );
          $form['resizable'] = array(
            '#type' => 'hidden',
            '#value' => 'true',
          );
          return $form;
        default:
          return array();
      }
      break;
    case 'render':
      switch ($attr['format']) {
        case 'image':
          $img_attributes = array(
            'title' => $attr['title'],
            'alt' => $attr['title'],
          );
          if ($attr['height']) {
            $img_attributes['height'] = $attr['height'];
          }
          if ($attr['width']) {
            $img_attributes['width'] = $attr['width'];
          }
          return '<img src="' . file_create_url($asset->filepath) . '" ' . drupal_attributes($img_attributes) . ' />';
        default:
          return theme('asset_render_default', $asset);
      }
      break;
    case 'preview':
      switch ($attr['format']) {
        case 'image':
          return theme('image', file_create_path($asset->filepath), '', '', array(
            'width' => '100',
          ), false);
        case 'link':
          return theme('asset_render_default', $asset);
      }
      break;
    case 'details':
      switch ($attr['format']) {
        case 'image':
          $info = image_get_info(file_create_path($asset->filepath));
          return array(
            t('Width') => $info['width'] . 'px',
            t('Height') => $info['height'] . 'px',
          );
      }
      return array();
    case 'img':
      switch ($attr['format']) {
        case 'image':
          return file_create_url($asset->filepath);
        case 'link':
        default:

          // when we get around to building icons.
          $icon = drupal_get_path('module', 'asset') . '/misc/icons/' . $asset->extension . '.png';
          if (file_exists($icon)) {
            return $icon;
          }

          // if all else fails send back a transparent gif so the default bg image shows
          return drupal_get_path('module', 'asset') . '/misc/transparent.gif';
      }
      break;
  }
}