You are here

imagefield_crop.module in Imagefield Crop 6

File

imagefield_crop.module
View source
<?php

// remove this for Drupal 7. see http://drupal.org/node/305645
defined('REQUEST_TIME') or define('REQUEST_TIME', $_SERVER['REQUEST_TIME']);

/**
* Implementation of hook_init().
*
*
Load required includes and css files.
*/
function imagefield_crop_init() {

  // field_file hooks and callbacks.
  module_load_include('inc', 'imagefield_crop', 'imagefield_crop_file');
  module_load_include('inc', 'imagefield_crop', 'imagefield_crop_widget');

  //  drupal_add_css(drupal_get_path('module', 'imagefield_crop') .'/imagefield_crop.css');
}

/**
 * Implementation of CCK's hook_widget_info().
 */
function imagefield_crop_widget_info() {
  $module_path = drupal_get_path('module', 'imagefield_crop');
  return array(
    'imagefield_crop_widget' => array(
      'label' => t('Image with cropping'),
      'field types' => array(
        'image',
        'filefield',
      ),
      'multiple values' => CONTENT_HANDLE_CORE,
      'callbacks' => array(
        'default value' => CONTENT_CALLBACK_CUSTOM,
      ),
      // callback for dynamic filefield widgets to determine
      // if this widget is appropriate for a file type.
      'suitability callback' => 'imagefield_handles_file',
      // description to use on forms to describe this widget.
      'description' => t('An edit widget for image files, including a crop interface.'),
    ),
  );
}

/**
 * Implementation of CCK's hook_widget_settings().
 *
 * Delegated to filefield.
 */
function imagefield_crop_widget_settings($op, $widget) {

  // make sure we have the functions as this may be called from update.php
  module_load_include('inc', 'imagefield_crop', 'imagefield_crop_widget');
  switch ($op) {
    case 'form':
      return imagefield_crop_widget_settings_form($widget);
    case 'validate':
      return imagefield_crop_widget_settings_validate($widget);
    case 'save':
      return imagefield_crop_widget_settings_save($widget);
  }
}

/**
 * Implementation of hook_widget().
 */
function imagefield_crop_widget(&$form, &$form_state, $field, $items, $delta = 0) {
  $element = imagefield_widget($form, $form_state, $field, $items, $delta);
  return $element;
}

/**
 * Implementation of hook_elements().
 */
function imagefield_crop_elements() {
  $elements = array();
  $elements['imagefield_crop_widget'] = array(
    // Indicate to FormAPI that this element needs processing and is not simply a render element.
    '#input' => TRUE,
    '#process' => array(
      'filefield_widget_process',
      'imagefield_widget_process',
      'imagefield_crop_widget_process',
    ),
    '#value_callback' => 'imagefield_crop_widget_value',
    '#element_validate' => array(
      'filefield_widget_validate',
      'imagefield_widget_validate',
      'imagefield_crop_widget_validate',
    ),
    '#description' => t('Changes made to the attachments are not permanent until you save this post.'),
  );
  return $elements;
}

/**
 * Implementation of hook_theme().
 */
function imagefield_crop_theme() {
  $theme = array(
    // imagefield_crop_widget form element type theme function.
    'imagefield_crop_widget' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'file' => 'imagefield_crop_widget.inc',
    ),
    // display dynamic preview
    'imagefield_crop_dynamic_preview' => array(
      'arguments' => array(
        'file' => NULL,
        'resolution' => 0,
        'preview_width' => 0,
      ),
    ),
    // display admin preview
    'imagefield_crop_widget_preview' => array(
      'arguments' => array(
        'item' => NULL,
      ),
    ),
    // display crop box
    'imagefield_crop_cropbox' => array(
      'arguments' => array(
        'file' => NULL,
        'alt' => '',
        'title' => '',
        'attributes' => NULL,
        'getsize' => TRUE,
        'id' => NULL,
      ),
    ),
    'imagefield_crop_edit_crop_image_row' => array(
      'arguments' => array(
        'element' => NULL,
      ),
    ),
    // ImageField formatter theme functions.
    'imagefield_crop_formatter_image_uncrop_plain' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'file' => 'imagefield_crop_formatter.inc',
    ),
    'imagefield_crop_formatter_image_uncroplink' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'file' => 'imagefield_crop_formatter.inc',
    ),
  );
  if (function_exists('imagecache_presets')) {
    foreach (imagecache_presets() as $preset) {
      $theme['imagefield_crop_formatter_' . $preset['presetname'] . '_default_uncropped'] = array(
        'arguments' => array(
          'element' => NULL,
          'function' => 'theme_imagecache_formatter_default',
        ),
        // Passing default value doesn't actually work! d'oh.
        'function' => 'theme_imagefield_crop_formatter_uncropped',
      );
      $theme['imagefield_crop_formatter_' . $preset['presetname'] . '_linked_uncropped'] = array(
        'arguments' => array(
          'element' => NULL,
          'function' => 'theme_imagecache_formatter_linked',
        ),
        'function' => 'theme_imagefield_crop_formatter_uncropped',
      );
      $theme['imagefield_crop_formatter_' . $preset['presetname'] . '_imagelink_uncropped'] = array(
        'arguments' => array(
          'element' => NULL,
          'function' => 'theme_imagecache_formatter_imagelink',
        ),
        'function' => 'theme_imagefield_crop_formatter_uncropped',
      );
      $theme['imagefield_crop_formatter_' . $preset['presetname'] . '_path_uncropped'] = array(
        'arguments' => array(
          'element' => NULL,
          'function' => 'theme_imagecache_formatter_path',
        ),
        'function' => 'theme_imagefield_crop_formatter_uncropped',
      );
      $theme['imagefield_crop_formatter_' . $preset['presetname'] . '_url_uncropped'] = array(
        'arguments' => array(
          'element' => NULL,
          'function' => 'theme_imagecache_formatter_url',
        ),
        'function' => 'theme_imagefield_crop_formatter_uncropped',
      );
    }
  }
  return $theme;
}

/**
 * Implementation of hook_field_formatter_info().
 *
 * imagecache formatters are named as $presetname_$style
 * $style is used to determine how the preset should be rendered.
 * If you are implementing custom imagecache formatters please treat _ as
 * reserved.
 *
 * @todo: move the linking functionality up to imagefield and clean up the default image
 * integration.
 */
function imagefield_crop_field_formatter_info() {
  $formatters = array();
  $formatters = array(
    'image_uncrop_plain' => array(
      'label' => t('Image uncropped'),
      'field types' => array(
        'filefield',
      ),
      'description' => t('Displays image files uncropped (only for imagefield_crop files).'),
    ),
    'image_uncroplink' => array(
      'label' => t('Image linked to image uncropped'),
      'field types' => array(
        'filefield',
      ),
      'description' => t('Links to image files uncropped (only for imagefield_crop files).'),
    ),
  );
  if (function_exists('imagecache_presets')) {
    foreach (imagecache_presets() as $preset) {
      $formatters[$preset['presetname'] . '_default_uncropped'] = array(
        'label' => t('@preset image (uncropped)', array(
          '@preset' => $preset['presetname'],
        )),
        'field types' => array(
          'filefield',
        ),
      );
      $formatters[$preset['presetname'] . '_linked_uncropped'] = array(
        'label' => t('@preset image linked to node (uncropped)', array(
          '@preset' => $preset['presetname'],
        )),
        'field types' => array(
          'filefield',
        ),
      );
      $formatters[$preset['presetname'] . '_imagelink_uncropped'] = array(
        'label' => t('@preset image linked to image (uncropped)', array(
          '@preset' => $preset['presetname'],
        )),
        'field types' => array(
          'filefield',
        ),
      );
      $formatters[$preset['presetname'] . '_path_uncropped'] = array(
        'label' => t('@preset file path (uncropped)', array(
          '@preset' => $preset['presetname'],
        )),
        'field types' => array(
          'filefield',
        ),
      );
      $formatters[$preset['presetname'] . '_url_uncropped'] = array(
        'label' => t('@preset URL (uncropped)', array(
          '@preset' => $preset['presetname'],
        )),
        'field types' => array(
          'filefield',
        ),
      );
    }
  }
  return $formatters;
}
function theme_imagefield_crop_widget_preview($item = NULL) {
  if (is_null($item) || empty($item['filepath'])) {
    return '<!-- link to default admin thumb -->';
  }
  $thumb_path = imagefield_file_admin_thumb_path($item);

  // A dummy query-string is added to filenames, to gain control over
  // browser-caching. The string changes on every update or full cache
  // flush, forcing browsers to load a new copy of the files, as the
  // URL changed.
  // This technique was copied from common.inc, see css_js_query_string
  // documentation
  $query_string = '?' . variable_get('imagefield_crop_query_string', REQUEST_TIME);
  return '<img src="' . file_create_url($thumb_path) . $query_string . '" />';
}

/**
 * Theme function to refactor the field element to use the uncropped
 * file in the imagecache formatter.
 *
 * @param $element
 *   The field element.
 * @param $function
 *   The theme function that should process the reformatted element.
 * @return
 *   The themed output from $function.
 */
function theme_imagefield_crop_formatter_uncropped($element, $function = '') {

  // Get the appropriate theme function name, if empty. Can't do anything
  // if there is no function name.
  if ($function || ($function = _imagefield_crop_formatter_theme_function($element['#formatter']))) {

    // Change the filepath for the item.
    $element['#item']['filepath'] = imagefield_crop_file_admin_crop_display_path($element['#item']);

    // Remove '_uncropped' from the formatter.
    $element['#formatter'] = substr($element['#formatter'], 0, -10);

    // Use function if specified, otherwise hack together the imagecache one.
    $element['#theme'] = $function ? $function : 'theme_imagecache_' . $element['#formatter'];

    // Should this call via 'theme($function, ...)' instead?
    return $function($element);
  }
}

/**
 * Helper function to return the appropriate theme function from a formatter.
 *
 * @param $formatter
 *   String formatter to derive the theme function to handle the element.
 * @return
 *   String function name.
 */
function _imagefield_crop_formatter_theme_function($formatter) {
  if (strpos($formatter, '_default_uncropped')) {
    return 'theme_imagecache_formatter_default';
  }
  if (strpos($formatter, '_linked_uncropped')) {
    return 'theme_imagecache_formatter_linked';
  }
  if (strpos($formatter, '_imagelink_uncropped')) {
    return 'theme_imagecache_formatter_imagelink';
  }
  if (strpos($formatter, '_path_uncropped')) {
    return 'theme_imagecache_formatter_path';
  }
  if (strpos($formatter, '_url_uncropped')) {
    return 'theme_imagecache_formatter_url';
  }
}

Functions

Namesort descending Description
imagefield_crop_elements Implementation of hook_elements().
imagefield_crop_field_formatter_info Implementation of hook_field_formatter_info().
imagefield_crop_init Implementation of hook_init().
imagefield_crop_theme Implementation of hook_theme().
imagefield_crop_widget Implementation of hook_widget().
imagefield_crop_widget_info Implementation of CCK's hook_widget_info().
imagefield_crop_widget_settings Implementation of CCK's hook_widget_settings().
theme_imagefield_crop_formatter_uncropped Theme function to refactor the field element to use the uncropped file in the imagecache formatter.
theme_imagefield_crop_widget_preview
_imagefield_crop_formatter_theme_function Helper function to return the appropriate theme function from a formatter.