I have a Drupal Commerce site running. On product level, users can create product variations by using the Inline Entity Form (like is done in most DC-sites).
I have an image field on the product content type and I have an image field on the product variation type. Both fields have ManualCrop enabled with the same settings.
However, other image styles are set available/required for both fields.

When I use the same image (in my example this beautiful koala), all goes well when creating the product. When however I want to edit this product and I hit the "edit" button on the Inline Entity Form or when I save an updated crop, the preview of the image duplicates over and over again (see state2 image for reference).

When I take a look in the HTML, the wrapping div is repeated over and over again (see attachment 3).

I think the issue is related to this issue and I quote:

This is due to a check on the manualcrop selections if a form has been submitted or not

The patch provided in this issue isn't applicable anymore...

Any thought or ideas?

UPDATE:
After some research I found out it has more to do with the duplicate presence of $fid in the manualcrop_croptool_process() function in manualcrop.helper.inc file on line 231.

CommentFileSizeAuthor
#51 manualcrop-duplicatepreview-2503175-51.patch4.23 KBnitheesh
#47 manualcrop-duplicatepreview-2503175-47.patch3.74 KBstefan.korn
#46 manualcrop_bug1.png143.11 KBfreed_paris
#45 interdiff-44-45.txt1.46 KBtanc
#45 manualcrop-duplicatepreview-2503175-45.patch20.46 KBtanc
#44 interdiff-41-44.txt9.14 KBtanc
#44 manualcrop-duplicatepreview-2503175-44.patch20.08 KBtanc
#42 manualcrop-duplicatepreview-2503175-42.patch15.04 KBmstrelan
#41 manualcrop-duplicatepreview-2503175-41.patch21.2 KBbirk
#40 manualcrop-duplicatepreview-2503175-40.patch43.25 KBbirk
#39 manualcrop-preview.png333.64 KBkasperg
#37 manualcrop-duplicatepreview-2503175-30.patch21.66 KBeglaw
#29 manualcrop-duplicatepreview-2503175-29.patch20.75 KBjefuri
#27 2503175-manualcrop-duplicatepreview-27.patch21.25 KBdavidsickmiller
#26 2503175-manualcrop-interdiff-25-26.txt429 bytesgeoffreyr
#26 2503175-manualcrop-duplicatepreview-26.patch21.22 KBgeoffreyr
#25 2503175-manualcrop-duplicatepreview-25.patch21.16 KBgeoffreyr
#24 2503175-manualcrop-duplicatepreview-24.patch19.1 KBgeoffreyr
#23 manualcrop-2503175-duplicatepreview-23.patch19.54 KBdavidsickmiller
#21 manualcrop-2503175-duplicatepreview-21.patch17.58 KBtoaster99
#17 manualcrop-2503175-duplicatepreview-17.patch8.21 KBgeoffreyr
#14 manualcrop-2503175-duplicatepreview-14.patch8.23 KBgeoffreyr
#13 duplicate_preview_with-2503175-13.patch7.67 KBtessa bakker
#11 duplicate_preview_with-2503175-11.patch7.88 KBtessa bakker
Edit_Shoes_Witte_schoenen_state1.png491.07 KBmichielkenis
Edit_Shoes_Witte_schoenen_state2.png676.36 KBmichielkenis
#1 Screen Shot 2015-06-09 at 17.41.25.png119.15 KBmichielkenis

Comments

michielkenis’s picture

Issue summary: View changes
Related issues: +#2063345: Support inline entity form
StatusFileSize
new119.15 KB
tessa bakker’s picture

Hi michielkenis,

Can you tell me the version number of your Media module?

Thanks!

michielkenis’s picture

Hi Tessa,

Thanks for your answer. The version I'm using is 7.x-2.0-alpha4+37-dev.
Although after some research myself, I think it has more to do with having the same $fid on the same page rather then the issue with an inline entity form.
I have to test more to be sure ofcourse...

michielkenis’s picture

Title: Duplicate preview with Inline Entity Form » Duplicate preview with same image twice on one page
Issue summary: View changes
michielkenis’s picture

After some research I found out it has more to do with the duplicate presence of $fid in the manualcrop_croptool_process() function in manualcrop.helper.inc file on line 231.

matthijs’s picture

As you already figured out, Manual Crop expects the fid to be unique in a form. This is needed to:

  • Find the associated thumbnail (the "bug" you're experiencing);
  • Determine the required crop styles;
  • Properly save/update existing crops and remove the ones that were cleared.

So I would guess you'll encounter some extra issues with this set-up... I don't think this can be fixed easily, isn't it possible to split out the forms?

michielkenis’s picture

Hi Matthijs,

Thanks for looking into this. I'm afraid splitting the form won't resolve this issue.
It's a commerce site were content managers can provide a lot (...) of images in the backend. There will be a good chance content managers will reuse these images on different places.
But if you decide this is a non-issue, I have to look for an alternative. After all, who uses the same image twice on the same product, right? :)

tessa bakker’s picture

Assigned: Unassigned » tessa bakker

Hi,

With some luck I will add a patch in a day or two, had the same issue with a site today.

marty2081’s picture

Same issue here with a site that has a node type that has two image fields to be able to select two different images. However the content editor selected the same image for both fields resulting in the reported issue. So , this situation can actually exist on production sites and is not a "non-issue".

tessa bakker’s picture

Patch is in progress.

tessa bakker’s picture

Title: Duplicate preview with same image twice on one page » Duplicate preview with same Media image twice on one page
Status: Active » Needs review
StatusFileSize
new7.88 KB

Here is the first patch, but it requires more testing.

Tested with:
* Media multiple fields, multiple images

Needs to be tested with:
* no Media fields (just core image field)
* Media + Multiple fields and multiple image styles
* ...?

Let me know what you think!

tessa bakker’s picture

Status: Needs review » Needs work

Hmm, spotted debug code on line 610 of the JS file :(

tessa bakker’s picture

Status: Needs work » Needs review
StatusFileSize
new7.67 KB

Without debug code..

geoffreyr’s picture

Hi,
I'm uploading a slightly modified version of the patch in #13 because I noticed that the JS event handler in manual-style-select that calls ManualCrop.showCropTool was missing the element ID. This change fixes up that event handler.

tessa bakker’s picture

Nice update Geoffreyr!

Tested the patches some more and here are some points to fix first, before we can call RTBC:
- ManualCrop Thumb list shows the label times the fid.
- Doesn't work with Field Collection

Extra test:
- Field Group (horizontal tabs and vertical tabs)

tessa bakker’s picture

Status: Needs review » Needs work

Argh .. status

geoffreyr’s picture

I noticed that when uploading a new file through the Media picker, Manual Crop would fail to find an ID for the field and throw an error. The following patch fixes this; I'm yet to determine if it fixes your issues with Field Collection though.

nitebreed’s picture

I can confirm the patch in #17 works for horizontal tabs

marty2081’s picture

Patch #17 works in our case.

alphex’s picture

Any update to this?
Applying the patch from #17 fixed the repeating images in the edit UI... but now, as I see, it doens't work with a field collection...

Also, maybe related.
The manual crops aren't sticking anymore.
The feedback you used to get with (with the parenthesis, saying "cropped") now doesn't show up.

The interface sends you to the crop screen, you can select what you want to crop, hit save... but it doesn't change the status in the drop down.

You can select the crops, and then hit save, but they don't show up on the front end.

Then when you EDIT the node, and look at the crops, they haven't saved.

Thank you.

toaster99’s picture

Found that the previews would disappear, styles wouldn't be marked as "(cropped)", and the crops weren't sticking with the above patches, this should fix that

nitebreed’s picture

The patch in #21 introduces a new problem for me. I'm getting this error multiple times:

Notice: Array to string conversion in manualcrop_croptool_validate() (regel 437 van C:\wamp\www\nhl.trunk\sites\all\modules\contrib\manualcrop\manualcrop.module).

Also I still have this error:

[image] must have a cropping selection for the [image_style] image style.

davidsickmiller’s picture

I also had the "Notice: Array to string conversion in manualcrop_croptool_validate()" error from patch #21.

I'm attaching a new patch that fixes this error. It also adds support for the case where one field using the image uses only one style (thus displaying a "crop" button) and the other field using the image uses multiple styles (thus displays a select element).

geoffreyr’s picture

Rerolled against latest DEV.
One thing I noticed about patch #23 was that changing the #parents array in manualcrop_croptool_process led to a really nasty form processing bug. That array gets fed to drupal_array_get_nested_value, which expects a single-level array of either integers or strings. Sending it an array of arrays made that function throw all kinds of horrible errors, especially when used in conjunction with the Media Browser.

geoffreyr’s picture

In case anyone is interested, here's an alternative to #24 that I put together that uses data-* attributes to indicate elements that Manual Crop's JS should use. These attributes pass in a JSON-encoded string containing parameters that each element should use. Not only will this keep in line with principles of unobtrusive Javascript, but it should also lower the likelihood of inadvertent typos in the onchange and onmousedown handlers. (I've hit this exact problem several times while working on this issue, and it's been a persistent enough bugbear for me to want to have to make this change.)

geoffreyr’s picture

StatusFileSize
new21.22 KB
new429 bytes

One more change to my patch in #25. Added a check to see if the $id was unset - in the case of using this together with Media module, this happened whenever I uploaded a new image to a Manual Crop-enabled field. This change ensures that there is an ID available at all times, regardless of circumstances.

davidsickmiller’s picture

I got the following error from #26 when saving a node with cropped images:

Notice: Undefined index: #id in _manualcrop_add_croptool() (line 419 of sites/all/modules/patched/manualcrop/manualcrop.helpers.inc).

It looks like the intent of the last change to patch #26 was to address this situation, but the empty() function wasn't used in a way that avoids the notice. I'm attaching a #27 that fixes this one problem.

#26 avoids a problem with my #23 that prevented the user from being able to save changes after clicking the Media Browser's edit button on the image. I'm also pleased to see that #26 still handles the case where one field using the image uses only one style (thus displaying a "crop" button) and the other field using the image uses multiple styles (thus displays a select element).

David_Rothstein’s picture

jefuri’s picture

This patch does not work on the dev version when using the same image ($fid) within a node. I fixed the patch to work with the latest dev version. Also this makes it work with multiple fields with the same style.

dqd’s picture

Status: Needs work » Needs review
dqd’s picture

Assigned: tessa bakker » Unassigned

Since Tecca stepped back a little bit and others chimed in too, we maybe should encourage others to chime in by removing "assigned to" from this issue atm?

ParisLiakos’s picture

  1. +++ sites/all/modules/contrib/manualcrop/manualcrop.helpers.inc	(revision )
    @@ -236,14 +236,22 @@
    +    if(!isset($form_state['manualcrop_data']['images'][$file->fid]))
    +    {
    ...
    +    else
    +    {
    
    @@ -408,6 +416,11 @@
    +  } else {
    
    +++ sites/all/modules/contrib/manualcrop/manualcrop.module	(revision )
    @@ -407,66 +407,76 @@
    +      foreach($image['element_parents'] as $parent)
    +      {
    ...
    +          if(!isset($selections[$fid]))
    +          {
    ...
    +          else
    +          {
    

    Coding standards

  2. +++ sites/all/modules/contrib/manualcrop/manualcrop.helpers.inc	(revision )
    @@ -236,14 +236,22 @@
    -      'element_parents' => $container['#parents'],
    +        'element_parents' => array($container['#parents']),
    

    can someone explain this change?

  3. +++ sites/all/modules/contrib/manualcrop/manualcrop.js	(revision )
    @@ -15,7 +15,7 @@
    -    $('.manualcrop-identifier-' + identifier, context).once('manualcrop-init', function() {
    +    $('.manualcrop-identifier-' + identifier, context).once('manualcrop-init', function () {
    
    @@ -25,13 +25,13 @@
    -  $('.manualcrop-cropdata', context).once('manualcrop-init', function() {
    +  $('.manualcrop-cropdata', context).once('manualcrop-init', function () {
    ...
    -  $('.manualcrop-button', context).once('manualcrop-init', function() {
    +  $('.manualcrop-button', context).once('manualcrop-init', function () {
    -    $(this).mousedown(function() {
    +    $(this).mousedown(function () {
    
    @@ -40,7 +40,7 @@
    -    $('.ajax-new-content', context).once('manualcrop-init', function() {
    +    $('.ajax-new-content', context).once('manualcrop-init', function () {
    
    @@ -61,13 +61,13 @@
    -  $('.modal-content', context).once('manualcrop-init', function() {
    +  $('.modal-content', context).once('manualcrop-init', function () {
    -    $(this).ready(function() {
    +    $(this).ready(function () {
    ...
    -
    +  ¶
    

    lets remove those changes, it just makes the patch harder to review

  4. +++ sites/all/modules/contrib/manualcrop/manualcrop.js	(revision )
    @@ -664,27 +663,44 @@
    +          if ($(this).val().lastIndexOf(' ' + Drupal.t('(cropped)')) == -1
    +              || $(this).val().lastIndexOf(' ' + Drupal.t('(cropped)')) != ($(this).val().length - Drupal.t('(cropped)').length - 1)) {
    +            $(this).val($(this).val() + ' ' + Drupal.t('(cropped)'));
    ...
    +          if ($(this).text().lastIndexOf(' ' + Drupal.t('(cropped)')) == -1
    +              || $(this).text().lastIndexOf(' ' + Drupal.t('(cropped)')) != ($(this).text().length - Drupal.t('(cropped)').length - 1)) {
    +            $(this).text($(this).text() + ' ' + Drupal.t('(cropped)'));
    ...
    +          if ($(this).val().lastIndexOf(' ' + Drupal.t('(cropped)')) != -1
    +              && $(this).val().lastIndexOf(' ' + Drupal.t('(cropped)')) == ($(this).val().length - Drupal.t('(cropped)').length - 1)) {
    +            $(this).val($(this).val().substr(0, ($(this).val().length - Drupal.t('(cropped)').length - 1)));
    ...
    +          if ($(this).text().lastIndexOf(' ' + Drupal.t('(cropped)')) != -1
    +              && $(this).text().lastIndexOf(' ' + Drupal.t('(cropped)')) == ($(this).text().length - Drupal.t('(cropped)').length - 1)) {
    +            $(this).text($(this).text().substr(0, ($(this).text().length - Drupal.t('(cropped)').length - 1)));
    

    Lets store Drupal.t('cropped') result somewhere, and $(this).text(), $(this).val()
    to avoid tens of unneeded function calls?
    Same for $(this)

maxplus’s picture

Hi,
for me #29 is solving this multiple-image-preview problem with the current dev version,

Thanks

letrotteur’s picture

With a new release of manualcrop a month ago, would love to see a working patch that apply cleanly to latest dev.

alphex’s picture

Just confirming #29 works when patching 1.6.

letrotteur’s picture

Patch assumes module is in sites/all/modules/contrib which is not always the case. Which makes it non-standard and hard to apply.

eglaw’s picture

StatusFileSize
new21.66 KB

#29 seems to work, only it's not a git-standard format, instead it seems to be a product of a phpStorm or something like that - it bears the IDEA git format, which is not always compliant. Drush make for example breaks upon the build.
This #30 patch is simply the same as #29 but in a git-patch format.

Works against version 1.6

nicodh’s picture

Hi,
#30 patch works for me patching 1.6
Thx

kasperg’s picture

StatusFileSize
new333.64 KB

The patch in #37 partly works for me with 1.6.

With the patch the preview is not shown at all.

birk’s picture

This patch file is broken, use #41
I've changed the id (in patch #39) to a class matching the field name instead. Assumes there's a field_name property in the element, this breaks use cases where manualcrop is used outside the Drupal field elements - which is not ideal.

The Media module (this might only be some version of Media) added an extra --widget to the id, so the toolOpener selector didn't match when multiple crops where enabled.

This also fixes the preview issue mentioned in #39.

birk’s picture

StatusFileSize
new21.2 KB

My previous patch wasn't created properly, this should fix that.

mstrelan’s picture

Patch in #41 seems ok but doesn't resolve the issue in #28 (which is #2711195: If two fields use the same manual crop style and the same image is reused for both, crops are lost on save). Here is a patch that combines the two together which seems to work for me.

tessa bakker’s picture

Status: Needs review » Needs work

#41 still won't allow multiple media fields with the same image file to be cropped, you can crop images, but the cropped style won't be saved, also the preview image is repeated and appended in the amount of the images that are identically used on the node as shown in #1

#42 doesn't apply to dev-branch

@ next patch uploader, please add interdiff.txt to show the changed lines.

tanc’s picture

Status: Needs work » Needs review
StatusFileSize
new20.08 KB
new9.14 KB

I painstakingly applied #42 by hand against current dev. This patch is just a re-roll of #42. I've added an interdiff from #41.

In my testing it certainly seems to fix the issue of multiple thumbnails appearing. It also saved the second instance of a media field with an image crop but not the crop from the first. I haven't thoroughly tested though.

tanc’s picture

I had missed a couple of important lines from the previous patch. This one now works nicely for me and solves the following two issues:

  • Duplicate/multiple previews after applying crop
  • Only once file's crop settings saved if multiple fields with the same file used
freed_paris’s picture

StatusFileSize
new143.11 KB

Hi,

I'm still have issue using Manual Crop (7.x-1.7) after using last patch (manualcrop-duplicatepreview-2503175-45.patch)
I'm using paragraphs.

Manual Crop preview bug

Fred

stefan.korn’s picture

I did also experience this issue.

While patch from #45 seems to be a very nice solution in most cases, I did still experience bugs in case field collection items with images where used and especially if multitple field collection items are allowed. Of course only in case the same image (same fid) is used several times as this whole issue is about this case only.

As @freed_paris experienced also issues with the patch from #45 together with paragraphs module, I suppose there would be more work to do to get #45 work with modules like field collection and paragraphs that create "field in field" scenarios. I looked in patch from #45, but find it very hard to get my head around all the changes that were done here and moreover found it difficult to rewrite this patch for the field collection use case.

So I did some work on a new patch that seem to solve the problem in my use case, where regular image fields (with media module) and field collection fields are used. I took 2711195 as the base and added some other changes.

My patch is a bit hacky, but it seems to solve the issue for me. The downside on this patch is that you lose the changing preview in the case when the same image is used several times. Also with this patch there will be no button used for the cropping even if only one style is selected (you will always get the select field). But for me this is fare more acceptable than the completely broken functionality within field collections. Maybe it will also work with paragraphs, but I have not tested this.

The patch from #45 seems to be a more clean solution than my patch. But hey, I needed something to fix this field collection mess ...

eduardo morales alberti’s picture

Patch #47 works for me at 1.6 version.

astolfivincent’s picture

Patch #45 works at version 7.x-1.7 when trying to use the same image in two different fields.

If you use the same image in the same field you'll still get the image double up issue, however (screenshot in #46 is what I'm referencing). I'm not sure how much of an issue this is, as most people probably aren't uploading the same image to the same field twice, but I thought it worth noting.

Before I applied the patch, on pages with many images & an instance or multiple instances of this issue it made it impossible to even update the node.

Per #48, I attempted rolling back to 1.6 and applying #47 to no avail, as it causes issues with the UI that render it pretty much unusable.

Site is using paragraphs but this issue was affecting non-paragraphs image fields as well.

stefan.korn’s picture

It should not be necessary to go back to 1.6 to apply the patch #47, I am using it with 1.7 too. It helped me with field collection and normal images in one node type. But if you have another configuration (i. e. paragraphs) it might have different implications, even for non-paragraph image fields when paragraph image fields are used together in one node type. The whole thing is quite complicated and if you hit one issue, another might pop up behind the next corner, as we can see in the issue history.

If #45 works out fine for you, than you should probably stick with this. I made #47 for a case where #45 did not suit, but tested it only in my specific configuration and therefore it might not suit other configurations.

nitheesh’s picture

I tried out the patch from #47 and it works great with paragraphs.

I'm updating the patch file with some small changes on JS file, otherwise, it will keep adding the label text on each cropping selection update.

mstrelan’s picture

#51 caused my browser to hang. #45 seems to do the job.