You are here

function _manualcrop_add_croptool in Manual Crop 7

Add the actual croptool to a form element.

Parameters

$element: The form element to add the croptool to.

$value: The element value storage.

$form_state: The form state array.

$fid: The file id.

$js_identifier: Unique javascript crop settings identifier.

$styles: Info of the active image styles, keyed by name.

$required: Array of required image styles.

$settings: Widget settings.

1 call to _manualcrop_add_croptool()
manualcrop_croptool_process in ./manualcrop.helpers.inc
Add a croptool to the form element. This extends the FAPI widget or simply adds a new form item to enable cropping in a regular form.

File

./manualcrop.helpers.inc, line 402
Helper functions for the Manual Crop module.

Code

function _manualcrop_add_croptool(&$element, $value, &$form_state, $fid, $js_identifier, $styles, $required = array(), $settings = array()) {

  // Get the fileinfo.
  $file_info = $form_state['manualcrop_data']['images'][$fid];

  // Overlay or inline cropping.
  if (!empty($settings['manualcrop_inline_crop'])) {
    $crop_type = 'inline';
  }
  else {
    $crop_type = 'overlay';
  }

  // Add a css class
  $element['#attributes']['class'][] = 'manualcrop-file-' . $fid . '-holder';

  // Original image for cropping.
  $element['manualcrop_' . $crop_type] = array(
    '#theme' => 'manualcrop_croptool_' . $crop_type,
    '#attributes' => array(
      'id' => array(
        'manualcrop-' . $crop_type . '-' . $fid,
      ),
      'class' => array(
        'manualcrop-' . $crop_type,
        'element-hidden',
      ),
    ),
    '#image' => array(
      'path' => $file_info['uri'],
      'alt' => $file_info['filename'],
      'width' => $file_info['width'],
      'height' => $file_info['height'],
      'attributes' => array(
        'class' => array(
          'manualcrop-image',
        ),
      ),
    ),
    '#crop_info' => !empty($settings['manualcrop_crop_info']),
    '#instant_preview' => !empty($settings['manualcrop_instant_preview']),
  );

  // Image style options.
  $style_options = array(
    '' => t('Select a style to crop'),
  );

  // Hidden fields to save the crop selection, one for each image style.
  foreach ($styles as $style_name => $info) {
    $element['manualcrop_selections'][$style_name] = array(
      '#type' => 'hidden',
      '#default_value' => isset($value['manualcrop_selections'][$style_name]) ? $value['manualcrop_selections'][$style_name] : '',
      '#attributes' => array(
        'id' => 'manualcrop-area-' . $fid . '-' . $style_name,
        'class' => array(
          'manualcrop-cropdata',
        ),
        'onchange' => 'ManualCrop.selectionStored(this, ' . $fid . ', \'' . $style_name . '\');',
      ),
    );
    $style_options[$style_name] = $info['label'];
  }

  // Default form element options.
  $defaults = array(
    '#weight' => isset($element['title']['#weight']) ? $element['title']['#weight'] : 1,
  );

  // Element to open the croptool.
  if (!empty($settings['manualcrop_thumblist'])) {

    // Thumbnail list, each image style is transformed in a clickable thumbnail.
    array_shift($style_options);
    foreach ($style_options as $style_name => $style_clean_name) {
      $prefix = '<span class="manualcrop-preview manualcrop-preview-' . $fid . ' manualcrop-preview-' . $fid . '-' . $style_name . '"><span class="manualcrop-preview-cropped"></span>';
      $suffix = '</span>';
      $style_options[$style_name] = theme('manualcrop_thumblist_image', array(
        'style' => $style_clean_name,
        'image' => $prefix . theme('image_style', array(
          'path' => $file_info['uri'],
          'alt' => $file_info['filename'],
          'width' => $file_info['width'],
          'height' => $file_info['height'],
          'style_name' => $style_name,
        )) . $suffix,
        'attributes' => array(
          'class' => array_merge(array(
            'manualcrop-style-thumb',
            'manualcrop-style-thumb-' . $fid,
            'manualcrop-style-thumb-' . $fid . '-' . $style_name,
            'manualcrop-style-preview-' . $fid,
            'manualcrop-style-preview-' . $fid . '-' . $style_name,
          ), in_array($style_name, $required) ? array(
            'manualcrop-style-required',
          ) : array()),
          'href' => 'javascript:void(0);',
          'onmousedown' => "ManualCrop.showCroptool('" . $js_identifier . "', '" . $style_name . "', " . $fid . ");",
          'onclick' => 'return false;',
        ),
      ));
    }
    $element['manualcrop_style'] = array_merge($defaults, array(
      '#markup' => theme('manualcrop_thumblist', array(
        'attributes' => array(
          'class' => array(
            'manualcrop-thumblist',
            'manualcrop-thumblist-' . $fid,
          ),
        ),
        'images' => $style_options,
      )),
    ));
  }
  elseif (count($style_options) == 2) {

    // Only one style, display a button.
    end($style_options);
    $style_name = key($style_options);
    $element['manualcrop_style'] = array_merge($defaults, array(
      '#type' => 'button',
      '#value' => t('Crop'),
      '#attributes' => array(
        'class' => array_merge(array(
          'manualcrop-style-button',
          'manualcrop-style-button-' . $fid,
        ), in_array($style_name, $required) ? array(
          'manualcrop-style-required',
        ) : array()),
        'onmousedown' => "ManualCrop.showCroptool('" . $js_identifier . "', '" . $style_name . "', " . $fid . ");",
        'onclick' => 'return false;',
      ),
      '#prefix' => '<div class="manualcrop-style-button-holder">',
      '#suffix' => '</div>',
    ));
  }
  else {

    // Style selection list.
    $element['manualcrop_style'] = array_merge($defaults, array(
      '#type' => 'select',
      '#title' => t('Manual Crop'),
      '#description' => t('Select the image style to crop, the corresponding cropping tool will open.'),
      '#options' => $style_options,
      '#multiple' => FALSE,
      '#attributes' => array(
        'class' => array(
          'manualcrop-identifier-' . $js_identifier,
          'manualcrop-style-select',
          'manualcrop-style-select-' . $fid,
        ),
        'onchange' => "ManualCrop.showCroptool('" . $js_identifier . "', this, " . $fid . ");",
      ),
    ));
  }

  // The FAPI widget can have a alt and/or title field, increase their weight.
  if (isset($element['alt']['#weight'])) {
    $element['alt']['#weight']++;
  }
  if (isset($element['title']['#weight'])) {
    $element['title']['#weight']++;
  }
}