You are here

function asset_asset_formatter in Asset 5.2

Same name and namespace in other branches
  1. 5 asset.module \asset_asset_formatter()
  2. 6 inc/asset.api.inc \asset_asset_formatter()

Implementation of hook_asset_type().

Related topics

File

./asset.module, line 948
Main module.

Code

function asset_asset_formatter($op = 'info', $asset = null, $attr = array()) {
  switch ($op) {
    case 'info':
      return array(
        'link' => array(
          'name' => t('Link'),
          'description' => t('A simple link to the file.'),
          'module' => 'asset',
        ),
        'image' => array(
          'name' => t('Image'),
          'description' => t('Standard image embedding'),
          'module' => 'asset',
        ),
      );
    case 'load':
      $formats = array();
      if ($asset->type == 'file') {
        $formats[] = 'link';
        if (strpos($asset->file['filemime'], 'image/') === 0) {
          $formats[] = 'image';
        }
      }
      return $formats;
    case 'img':
      $path = $asset->file['filepath'];
      return $path;
    case 'render':
      if ($attr['format'] == 'image') {
        $title = $attr['title'] ? $attr['title'] : $asset->title;
        $alt = $attr['alt'] ? $attr['alt'] : $asset->title;
        $getsize = $attr['height'] || $attr['width'] ? FALSE : TRUE;
        $attributes = $getsize ? array() : array(
          'height' => $attr['height'],
          'width' => $attr['width'],
        );
        return theme('image', $asset->file['filepath'], $alt, $title, $attributes, $getsize);
      }
      else {
        $text = $attr['text'] ? $attr['text'] : $asset->title;
        $link_attr = array();
        if ($attr['title']) {
          $link_attr['title'] = check_plain($attr['title']);
        }
        return l($text, $asset->file['filepath'], $link_attr);
      }
    case 'form':
      if ($attr['format'] == 'image') {
        $form['alt'] = array(
          '#type' => 'textfield',
          '#title' => t('Alt Text'),
          '#default_value' => $attr['alt'] ? $attr['alt'] : $asset->title,
          '#description' => t('Alt attribute for the image'),
        );
        $form['title'] = array(
          '#type' => 'textfield',
          '#title' => t('Title'),
          '#default_value' => $attr['title'] ? $attr['title'] : $asset->title,
          '#description' => t('Title attribute for the image'),
        );
        list($width, $height, $type, $image_attributes) = @getimagesize($asset->file['filepath']);
        $form['width'] = array(
          '#type' => 'textfield',
          '#title' => t('Height'),
          '#default_value' => $attr['width'] ? $attr['width'] : $width,
          '#size' => 5,
          '#field_suffix' => 'px',
        );
        $form['height'] = array(
          '#type' => 'textfield',
          '#title' => t('Height'),
          '#default_value' => $attr['height'] ? $attr['height'] : $height,
          '#size' => 5,
          '#field_suffix' => 'px',
        );
        $form['align'] = array(
          '#type' => 'select',
          '#title' => t('Alignment'),
          '#default_value' => $attr['align'] ? $attr['align'] : 'none',
          '#options' => array(
            'none' => t('None'),
            'left' => t('Left'),
            'right' => t('Right'),
          ),
        );
      }
      else {
        $form['text'] = array(
          '#type' => 'textfield',
          '#title' => t('Link Text'),
          '#default_value' => $attr['text'] ? $attr['text'] : $asset->title,
          '#description' => t('The text to display in the link.'),
        );
        $form['title'] = array(
          '#type' => 'textfield',
          '#title' => t('Title'),
          '#default_value' => $attr['title'] ? $attr['title'] : '',
          '#description' => t('Title attribute for the link.  In most browsers this will be displayed in the hover tooltip.'),
        );
      }
      return $form;
  }
}