diff --git a/manualcrop.helpers.inc b/manualcrop.helpers.inc index 5fb0e79..93f1e95 100644 --- a/manualcrop.helpers.inc +++ b/manualcrop.helpers.inc @@ -236,7 +236,7 @@ function manualcrop_croptool_process(&$form, &$form_state, &$element, $file, $se // Save some image data to improve processing. $image = image_get_info($file->uri); - $form_state['manualcrop_data']['images'][$file->fid] = array( + $form_state['manualcrop_data']['images'][$file->fid][] = array( 'uri' => $file->uri, 'filename' => $file->filename, 'width' => $image['width'], @@ -407,7 +407,7 @@ function _manualcrop_attach_files(&$element = NULL) { */ 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]; + $file_info = reset($form_state['manualcrop_data']['images'][$fid]); // Overlay or inline cropping. if (!empty($settings['manualcrop_inline_crop'])) { @@ -512,7 +512,7 @@ function _manualcrop_add_croptool(&$element, $value, &$form_state, $fid, $js_ide '#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())), + 'class' => array_merge(array('manualcrop-style-button', 'manualcrop-style-button-' . $fid . '-' . $style_name), (in_array($style_name, $required) ? array('manualcrop-style-required') : array())), 'onmousedown' => "ManualCrop.showCroptool('" . $js_identifier . "', '" . $style_name . "', " . $fid . ");", 'onclick' => 'return false;', ), diff --git a/manualcrop.js b/manualcrop.js index 148460e..c8bed1c 100644 --- a/manualcrop.js +++ b/manualcrop.js @@ -209,7 +209,7 @@ ManualCrop.showCroptool = function(identifier, style, fid) { } else { // Hide the crop button. - $('.manualcrop-style-button-' + fid).hide(); + $('.manualcrop-style-button-' + fid + '-' + styleName).hide(); } // Append the cropping area (last, to prevent that "_11" is undefined). @@ -591,26 +591,26 @@ ManualCrop.selectionStored = function(element, fid, styleName) { var selection = $(element).val(); ManualCrop.isLoaded('.manualcrop-file-' + fid + '-holder', function() { - var previewHolder = $('.manualcrop-preview-' + fid + '-' + styleName + ' .manualcrop-preview-cropped'); + var previewHolder = $(element).closest(':has(.manualcrop-preview-' + fid + '-' + styleName + ')').find('.manualcrop-preview-' + fid + '-' + styleName + ' .manualcrop-preview-cropped'); if (!previewHolder.length) { - previewHolder = $('.manualcrop-preview-' + fid + ' .manualcrop-preview-cropped'); + previewHolder = $(element).closest(':has(.manualcrop-preview-' + fid + ')').find('.manualcrop-preview-' + fid + ' .manualcrop-preview-cropped'); } - var defaultPreview = $('.manualcrop-preview-' + fid + '-' + styleName + ' > img'); + var defaultPreview = $(element).closest(':has(.manualcrop-preview-' + fid + '-' + styleName + ')').find('.manualcrop-preview-' + fid + '-' + styleName + ' > img'); if (!defaultPreview.length) { - defaultPreview = $('.manualcrop-preview-' + fid + ' > img'); + defaultPreview = $(element).closest(':has(.manualcrop-preview-' + fid + ')').find('.manualcrop-preview-' + fid + ' > img'); } // Change the elements if Media is detected. - var media = $('.manualcrop-preview-' + fid + ' .media-item[data-fid] .media-thumbnail'); + var media = $(element).closest(':has(.manualcrop-preview-' + fid + ')').find('.manualcrop-preview-' + fid + ' .media-item[data-fid] .media-thumbnail'); if (media.length) { media.prepend(previewHolder); previewHolder = media.find('.manualcrop-preview-cropped'); - defaultPreview = $('.manualcrop-preview-' + fid + ' .media-item[data-fid] .media-thumbnail > img'); + defaultPreview = media.children('img'); } - var toolOpener = $('.manualcrop-style-select-' + fid + " option[value='" + styleName + "'], .manualcrop-style-button-" + fid + ', .manualcrop-style-thumb-' + fid + '-' + styleName + ' .manualcrop-style-thumb-label'); + var toolOpener = $('.manualcrop-style-select-' + fid + " option[value='" + styleName + "'], .manualcrop-style-button-" + fid + '-' + styleName + ', .manualcrop-style-thumb-' + fid + '-' + styleName + ' .manualcrop-style-thumb-label'); var hasClass = toolOpener.hasClass('manualcrop-style-cropped'); if (previewHolder.length && previewHolder.children().length) { @@ -647,7 +647,7 @@ ManualCrop.selectionStored = function(element, fid, styleName) { var scaleY = resized.height / selection.height; // Get the original image and its dimensions. - var originalImage = $('#manualcrop-overlay-' + fid + ' img.manualcrop-image, #manualcrop-inline-' + fid + ' img.manualcrop-image'); + var originalImage = previewHolder.closest(':has(#manualcrop-overlay-' + fid + ', #manualcrop-inline-' + fid + ')').find('#manualcrop-overlay-' + fid + ' img.manualcrop-image, #manualcrop-inline-' + fid + ' img.manualcrop-image'); var dimensions = ManualCrop.getImageDimensions(originalImage); // Calculate the new width and height using the full image. diff --git a/manualcrop.module b/manualcrop.module index 5640ebb..2afd2af 100644 --- a/manualcrop.module +++ b/manualcrop.module @@ -406,67 +406,71 @@ function manualcrop_croptool_validate($element, &$form_state) { $selections = &$form_state['manualcrop_data']['selections']; // Loop trough the positions to get the Manual Crop data. - foreach ($form_state['manualcrop_data']['images'] as $fid => $image) { - $value = drupal_array_get_nested_value($form_state['values'], $image['element_parents']); - - if (!empty($value['manualcrop_selections'])) { - // Create a selections array for the current image, the first element is - // the path to the original image, needed for flushing the cache. - $selections[$fid] = array( - 'path' => $image['uri'], - 'styles' => array_fill_keys(array_keys($value['manualcrop_selections']), FALSE), - ); - - // Indicates if an error on the form element has been set. - $error_set = FALSE; - - // Loop over the selections. - foreach ($value['manualcrop_selections'] as $style_name => $crop) { - // Get the clean style name. - $clean_style_name = _manualcrop_image_style_label($style_name); - - // Get the element key. - if (!$error_set) { - $element_key = implode('][', $image['element_parents']) . '][manualcrop_style'; - } - else { - $element_key = implode('][', $image['element_parents']) . '][manualcrop_selections][' . $style_name; - } + foreach ($form_state['manualcrop_data']['images'] as $fid => $images) { + foreach ($images as $image) { + $value = drupal_array_get_nested_value($form_state['values'], $image['element_parents']); + + if (!empty($value['manualcrop_selections'])) { + // Create a selections array for the current image, the first element + // is the path to the original image, needed for flushing the cache. + $selections[$fid] = array( + 'path' => $image['uri'], + // Retain any styles that are already present from previous + // references to the image on the same form. + 'styles' => array_merge(array_fill_keys(array_keys($value['manualcrop_selections']), FALSE), isset($selections[$fid]['styles']) ? $selections[$fid]['styles'] : array()), + ); + + // Indicates if an error on the form element has been set. + $error_set = FALSE; + + // Loop over the selections. + foreach ($value['manualcrop_selections'] as $style_name => $crop) { + // Get the clean style name. + $clean_style_name = _manualcrop_image_style_label($style_name); + + // Get the element key. + if (!$error_set) { + $element_key = implode('][', $image['element_parents']) . '][manualcrop_style'; + } + else { + $element_key = implode('][', $image['element_parents']) . '][manualcrop_selections][' . $style_name; + } - if (!empty($crop)) { - // Validate the crop selection format. - if (preg_match('/^([0-9]+\|){3}[0-9]+$/', $crop)) { - $crop = array_map('intval', explode('|', $crop)); - - // Check position and minimum dimensions. - if ($crop[0] >= 0 && $crop[1] >= 0 && $crop[2] > 0 && $crop[3] > 0) { - // Check if the selections fits on the image. - if (($crop[0] + $crop[2]) <= $image['width'] && ($crop[1] + $crop[3]) <= $image['height']) { - $selections[$fid]['styles'][$style_name] = array( - 'x' => $crop[0], - 'y' => $crop[1], - 'width' => $crop[2], - 'height' => $crop[3], - ); - continue; + if (!empty($crop)) { + // Validate the crop selection format. + if (preg_match('/^([0-9]+\|){3}[0-9]+$/', $crop)) { + $crop = array_map('intval', explode('|', $crop)); + + // Check position and minimum dimensions. + if ($crop[0] >= 0 && $crop[1] >= 0 && $crop[2] > 0 && $crop[3] > 0) { + // Check if the selections fits on the image. + if (($crop[0] + $crop[2]) <= $image['width'] && ($crop[1] + $crop[3]) <= $image['height']) { + $selections[$fid]['styles'][$style_name] = array( + 'x' => $crop[0], + 'y' => $crop[1], + 'width' => $crop[2], + 'height' => $crop[3], + ); + continue; + } } } - } - // Invalid crop selection. - $error_set = TRUE; - form_set_error($element_key, t('The crop selection for %filename (@style image style) is invalid, please clear it or reselect.', array( - '@style' => $clean_style_name, - '%filename' => $image['filename'] - ))); - } - elseif (in_array($style_name, $image['required_styles'], TRUE)) { - // Uncropped required style. - $error_set = TRUE; - form_set_error($element_key, t('%filename must have a cropping selection for the @style image style.', array( - '@style' => $clean_style_name, - '%filename' => $image['filename'] - ))); + // Invalid crop selection. + $error_set = TRUE; + form_set_error($element_key, t('The crop selection for %filename (@style image style) is invalid, please clear it or reselect.', array( + '@style' => $clean_style_name, + '%filename' => $image['filename'] + ))); + } + elseif (in_array($style_name, $image['required_styles'], TRUE)) { + // Uncropped required style. + $error_set = TRUE; + form_set_error($element_key, t('%filename must have a cropping selection for the @style image style.', array( + '@style' => $clean_style_name, + '%filename' => $image['filename'] + ))); + } } } }