You are here

function imagecrop_process_form_element in Image javascript crop 7

Process function for imagecrop-enabled fields.

1 string reference to 'imagecrop_process_form_element'
imagecrop_element_info_alter in ./imagecrop.module
Implements hook_element_info_alter().

File

./imagecrop.module, line 311
Provides a javascript toolbox through an imagecache action.

Code

function imagecrop_process_form_element($element) {

  // Leave alone if we are on the field ui settings.
  if (!isset($element['#field_name']) || $element['#id'] == 'edit-picture-upload') {
    return $element;
  }
  $imagecrop = new ImageCrop();
  $media_field = $element['#type'] == 'media';
  $new_element = isset($element['#file']);
  if ($new_element) {

    // Type is gone in latest imagefield.
    if ($element['#file']) {
      if (isset($element['#file']->type)) {
        $mime = $element['#file']->type;
      }
      else {
        $mime_parts = explode('/', $element['#file']->filemime);
        $mime = array_shift($mime_parts);
      }
      if ($mime == 'image') {
        $imagecrop
          ->setFile($element['#file']);
      }
    }
  }
  else {
    $new_element = FALSE;
    if (!empty($element['fid']['#value'])) {
      $file = file_load($element['fid']['#value']);

      // Only add when the file is an image
      if ($file->type == 'image') {
        $imagecrop
          ->loadFile($element['fid']['#value'], TRUE);
      }
    }
  }

  // Leave alone if no access.
  global $user;
  if (!$imagecrop
    ->hasUserAccess($user)) {
    return $element;
  }
  if ($element['#entity_type'] == 'file') {
    $enabled_styles = array_keys(get_imagecrop_styles());
  }
  else {
    $enabled_styles = array_keys(imagecrop_get_fields_enabled_styles($element['#entity_type'], $element['#field_name'], $element['#bundle']));
  }

  // Leave element when no image styles are found for current field.
  if (count($enabled_styles) == 0) {
    return $element;
  }
  $variables = array(
    'styles' => $enabled_styles,
    'js_file' => 'imagecrop_field.js',
  );
  if ($new_element && !$media_field) {
    if (!empty($element['fid']['#value'])) {
      $variables['fid'] = $element['fid']['#value'];
      $variables['text'] = $media_field ? t('Crop media') : t('Crop image');
      $element['remove_button']['#prefix'] = imagecrop_linkitem($element, $variables);
    }
    else {

      // Make sure colorbox is loaded
      $popup_link_function = variable_get('imagecrop_popup', 'imagecrop_jquery_dialog');
      if ($popup_link_function == 'imagecrop_load_colorbox' && module_exists('colorbox')) {
        imagecrop_load_colorbox();
      }
    }
  }
  else {
    $variables['text'] = t('Crop media');
    if (empty($element['fid']['#value'])) {
      $variables['fid'] = 0;
      $variables['display'] = 'none';
    }
    else {
      $variables['fid'] = $element['fid']['#value'];
      $variables['display'] = 'inline-block';
    }
    $element['#attributes']['class'][] = 'imagecrop-media';

    // Old versus new version of media
    if (isset($element['remove'])) {
      $element['remove']['#prefix'] = imagecrop_linkitem($element, $variables);
    }
    else {
      $variables['js_file'] = 'imagecrop_field_media_v1.js';
      $element['preview']['#suffix'] .= imagecrop_linkitem($element, $variables);
    }
  }
  $element['#attributes']['class'][] = 'imagecrop-widget';
  return $element;
}