You are here

function imagefield_crop_widget_process in Imagefield Crop 7

Same name and namespace in other branches
  1. 6 imagefield_crop_widget.inc \imagefield_crop_widget_process()
  2. 7.3 imagefield_crop.module \imagefield_crop_widget_process()
  3. 7.2 imagefield_crop.module \imagefield_crop_widget_process()

An element #process callback for the imagefield_crop field type.

1 string reference to 'imagefield_crop_widget_process'
imagefield_crop_field_widget_form in ./imagefield_crop.module
Implements hook_field_widget_form().

File

./imagefield_crop.module, line 247
Provide a widget to crop uploaded image.

Code

function imagefield_crop_widget_process($element, &$form_state, $form) {
  $item = $element['#value'];
  $item['fid'] = $element['fid']['#value'];
  $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
  $settings = $instance['settings'];
  $widget_settings = $instance['widget']['settings'];
  $element['#theme'] = 'imagefield_crop_widget';
  $path = drupal_get_path('module', 'imagefield_crop');
  $element['#attached']['js'][] = "{$path}/Jcrop/js/jquery.Jcrop.js";

  // We must define Drupal.behaviors for ahah to work, even if there is no file.
  $element['#attached']['js'][] = "{$path}/imagefield_crop.js";
  $element['#attached']['css'][] = "{$path}/Jcrop/css/jquery.Jcrop.css";

  // Made a configuration since this is something that your local theme might not need.
  if (variable_get('imagefield_crop_max_width_fix', TRUE)) {
    $element['#attached']['css'][] = $path . '/css/imagefield-zenfix.css';
  }
  if ($element['#file']) {
    $file_to_crop = _imagefield_crop_file_to_crop($element['#file']->fid);
    $element['cropinfo'] = _imagefield_add_cropinfo_fields($element['#file']->fid);
    list($res_w, $res_h) = explode('x', $widget_settings['resolution']);
    list($crop_w, $crop_h) = explode('x', $widget_settings['croparea']);
    $element['preview'] = array(
      '#type' => 'markup',
      // This is used by the #process function.
      '#file' => $file_to_crop,
      '#process' => array(
        'imagefield_crop_widget_preview_process',
      ),
      '#theme' => 'imagefield_crop_preview',
      '#description' => t('Image preview <strong>(@res_wpx x @res_hpx)</strong>', array(
        '@res_w' => $res_w,
        '@res_h' => $res_h,
      )),
      '#markup' => theme('image', array(
        'path' => $element['#file']->uri,
        'getsize' => FALSE,
        'attributes' => array(
          'class' => 'preview-existing',
        ),
      )),
    );
    $element['cropbox'] = array(
      '#markup' => theme('image', array(
        'path' => $file_to_crop->uri,
        'attributes' => array(
          'class' => 'cropbox',
          'id' => $element['#id'] . '-cropbox',
        ),
      )),
      '#description' => t('Click on the image and drag to mark how the image will be cropped.'),
    );
    $settings = array(
      $element['#id'] => array(
        'box' => array(
          'ratio' => $res_h ? $widget_settings['enforce_ratio'] * $res_w / $res_h : 0,
          'box_width' => $crop_w,
          'box_height' => $crop_h,
        ),
        'minimum' => array(
          'width' => $widget_settings['enforce_minimum'] ? $res_w : NULL,
          'height' => $widget_settings['enforce_minimum'] ? $res_h : NULL,
        ),
      ),
    );
    $element['#attached']['js'][] = array(
      'data' => array(
        'imagefield_crop' => $settings,
      ),
      'type' => 'setting',
      'scope' => 'header',
    );
  }

  // Prepend submit handler to remove button.
  array_unshift($element['remove_button']['#submit'], 'imagefield_crop_widget_delete');
  return $element;
}