I have a content type that is successfully using reference option limit. After adding a file field to this content type and then uploading an attachment, the attachment appears to work successfully but shows this error message:
Notice: Undefined index: field_job_posting_company in reference_option_limit_form_alter() (line 285 of C:\Users\andrew\Web Sites\Drupal Sites\Retained Search\sites\all\modules\reference_option_limit\reference_option_limit.module)
If I use the "media" widget instead of "file" then I do not receive this error.
Just wanted to bring this to the attention of the community- maybe it is an easy fix or something wrong with my setup but maybe this is a symptom of something in the code not being compatible with other modules.
| Comment | File | Size | Author |
|---|---|---|---|
| #29 | shot fields.png | 70.7 KB | pmichelazzo |
| #24 | 1649470.24.reference_option_limit.filefields-ajax.patch | 5.44 KB | joachim |
| #20 | img1.jpg | 31.19 KB | 3cwebdev |
| #20 | img2.jpg | 37.19 KB | 3cwebdev |
| #17 | 1649470.reference_option_limit.file-widget-ajax-error.patch | 900 bytes | joachim |
Comments
Comment #1
joachim commented> After adding a file field to this content type and then uploading an attachment
Do you mean that the edit form gets refreshed? I've seen similar problems with forms updating via AJAX.
Comment #2
reych9 commentedI have the following errors when file is uploaded.
Notice: Undefined index: field_centro en reference_option_limit_form_alter() (línea 309 de K:\xampp\htdocs\sitio\sites\all\modules\reference_option_limit\reference_option_limit.module).
Warning: Invalid argument supplied for foreach() en reference_option_limit_form_alter() (línea 309 de K:\xampp\htdocs\sitio\sites\all\modules\reference_option_limit\reference_option_limit.module).
Notice: Undefined index: field_centro en reference_option_limit_form_alter() (línea 351 de K:\xampp\htdocs\sitio\sites\all\modules\reference_option_limit\reference_option_limit.module).
and ilegal option when i try to save the entity.
any help??
Comment #3
joachim commentedIt's probably because the form is getting refreshed and reloaded when you upload.
Just to clarify: how do you upload the file? Do you press the 'Upload' button or just browse it and then save the whole form?
Comment #4
reych9 commentedI think the same as you, but i don't know how to fix it. yes, i press the upload button.
I have the errors above the field and not in the begin of the page.
Here are screenshots
thanks.
Comment #5
reych9 commentedhelp please...
Comment #6
reych9 commentedI think the same as you, but i don't know how to fix it. yes, i press the upload button.
I have the errors above the field and not in the begin of the page.
sorry if I repeat the post, but I really need solve this problem, I love this module and I need It
thanks a lot
Comment #7
joachim commentedPlease don't keep posting the same comment.
Yes, there is a bug here.
Unfortunately, I suspect this is going to be a complex thing to fix. I don't require this functionality in any of my paid projects, and I don't have enough free time to fix it in my personal time.
If you really need this soon, you either need to fix this yourself, or hire a developer to do it for you.
Comment #8
3cwebdev commentedI found a simple fix for the issue on my setup that was preventing file/image fields from submitting properly due to the form refresh.
I changed the following line from:
to:
It's a very simple fix but seems to do the trick. Can anyone else test this?
joachim - once tested, can this be applied to an update of the module?
Comment #9
joachim commentedCould you post that change as a patch please?
Comment #10
3cwebdev commentedAfter further testing I found that my suggestion only fixed the errors that appears while attaching a file/image but did not address the issue of losing the limited options that were already selected before submitting a file/image. It's not pretty but this solution seems to fix both of the issues now. It simply tried to determine if the form is a file/image form in this module's hook_form_alter and then bypasses the rest of the logic if it is.
I am not experienced in making patched, if this appears to be a viable solution then perhaps someone can make the patch...
To fix: insert the following code right after these lines:
EXISTING CODE
NEW CODE TO INSERT
END RESULT
Comment #11
joachim commentedHow do you know the first field in the form is the file field? It could be anything!
BTW, there is help with patches here: http://drupal.org/patch
Comment #12
3cwebdev commentedRight. I've struggled with finding a way to determine if the submitted form is in fact a file/image field form. After working on it for half the day this is the best solution I could come up with. From my testing it does appear that a file/image form will contain a field type value in the first array. I know this isn't pretty and may not work in all setups but it is the only way I could find to make it work and it does fix both issues.
If someone can find a better way to check the $form_state['values'] to find if it's a file/image field then please advise.
Comment #13
joachim commentedFirst off, it's an entity form, always, because that's what our module works with.
I would dsm($form), and see what's in there. Possible lines of attack:
- IIRC some FieldAPI things have an array that is a list of all the fields
- alternatively get the entity type and bundle from the form, and use FieldAPI to get yourself of all fields on that instance
Next thing is that to do in that circumstance:
What does bypassing the rest of the function accomplish?
Comment #14
3cwebdev commentedWhat does bypassing the rest of the function accomplish?
I should clarify part of the symptom: the second part of the issue happens when a submitted form fails validation and there is a file/image field on the form. reference_option_limit_form_alter() is called more than once. Once for the regular form and then once for each file/image field. The hook_form_alter function is triggered for each of these forms because they each have the 'reference_option_limit' value on them. However, when the function runs on the file/image form it causes the option limited selection that had already been set to be lost (see second image on post #4 above).
By detecting if the form is a file/image field form then we can simply bypass the rest of the function preventing it from losing the previous selection.
I know this isn't graceful but it does seem to work. I've looked at the $form and $form_state objects extensively and haven't found a way to detect if it's a file/image field form yet. I will give it another crack in a couple of days though, as soon as I have time.
Comment #15
joachim commentedAhh... thanks for the explanation.
Does $form look the same both times?
If $form is different, we maybe need to think about changing the way $form_state['reference_option_limit'] works. For instance, we could store the #parents of the element we want to act on, and therefore be sure to only act when it's there.
Comment #16
3cwebdev commentedUpdate:
After looking into it further my current solution did not work properly. Besides not being a graceful solution I also discovered that although it did remove the errors and fixed the issue of losing the filtered option that was selected, after submitting a form with a file widget that failed validation it was losing the selective filtering and would then show all of the options available.
I was able to come up with another solution the involved storing the $form_state values in the reference_option_limit variable and only updating them if the $field_name key exists in the $form_state values. This allows easy and fool proof (as far as I can tell) detection of a file widget form.
Now the reference_option_limit_form_alter() function works on the values stored in the reference_option_limit ['values'] element instead of the actual $form_state['values'] element (because those values could be from the file widget) and everything seems to work perfect now.
I will try to get a patch up in the next couple of days.
Comment #17
joachim commented> only updating them if the $field_name key exists in the $form_state values.
That sounds like a good approach.
I found the time to have a quick look at this. The problem stems from the fact that the file and image widgets quite correctly use #limit_validation_errors on their ajax buttons, but the form rebuild obviously means our form_alter gets called... and #limit_validation_errors, as I learned recently, directly affects which parts of the form input gets processed and put into $form_state['values'].
The upshot is that we should be more careful about our test to see if we're in the AJAX part of the flow, exactly as you suggested earlier on.
Here's your earlier suggested change as a patch.
> After further testing I found that my suggestion only fixed the errors that appears while attaching a file/image but did not address the issue of losing the limited options that were already selected before submitting a file/image.
I can't reproduce that second problem. Here are my steps:
- go to add a new node
- choose a country taxonomy term -> the cities appear limited
- choose a file and click 'upload' -> the file is uploaded. My country and city selection are fine.
Comment #18
3cwebdev commentedTo reproduce the second problem you must:
- go to add a new node
- choose a country taxonomy term -> the cities appear limited
- choose a file and click 'upload' -> the file is uploaded
-also make sure a required field is NOT filled in so that the submitted form will fail validation
Now the city limited field will either be completely empty or may have all impossible options (depending if fix was applied).
Comment #19
joachim commented> -also make sure a required field is NOT filled in so that the submitted form will fail validation
Nope, it's working for me fine with that too -- I have both the node title and a text field required and blank.
Comment #20
3cwebdev commentedHmmmm. Sounds like you're doing the same thing as me to test but we're getting different results. To be sure, I just downloaded the module again and applied the patch but I still get the symptom.
See the attached screenshots. "Sub Type' is limited by "Main Type". If I select the main type and then select the sub type (limited field) and also upload an image then submit the form that fails validation I then lose the sub type field info.
To produce, I:
-Selected my main type field value
-Selected my sub type limited option value
-Selected and uploaded an image
-Submit form without filling in all required forms
-There are now no options for the sub type
Could you try to reproduce again? Your fix is obviously much simpler but it doesn't work for me. I have a more complicated fix but I'd like to verify that others are seeing the same symptom.
Comment #21
joachim commented> -There are now no options for the sub type
Ah right. I misunderstood you; I thought the subtype values went missing when you uploaded, not on submit.
So we really have two problems, which are possibly related:
1. Error messages when you hit the upload button on a file or image widget (or indeed, I suspect, any other AJAX process on the form)
2. The values on the subtype go missing if there was a later AJAX process
Comment #22
joachim commentedProblem 2 is going to be caused by something to do with form_state['values'].
One thing I've found is that when you hit upload, only the file or image field's values are sent, because of the #limit_validation_errors on that widget. Which is normal, but we might not be taking that possibility into account.
Comment #23
joachim commented> -There are now no options for the sub type
Yup, there's some sort of tandem effect going on here.
During the filefield AJAX, there were no form values for our fields, and so we didn't output options for the dependent field in the rebuilt form (you can't see this effect, because only the filefield part of the form is returned by the AJAX).
Then when we validate the whole form, we don't have the options for the dependent field. Hence our form value for the radio button/select in the dependent widget is lost.
In fact, I get:
> An illegal choice has been detected. Please contact the site administrator.
Do you get that too?
Comment #24
joachim commentedI think I've figured it out.
Can you try this patch please?
(See comments in the patch for an explanation of what's going on.)
Comment #25
pmichelazzoJoachim,
I try to use your patch but I'm still receiving a message like:
Notice: Undefined index: storage em reference_option_limit_form_alter() (line 361 of /var/www/sites/all/modules/reference_option_limit/reference_option_limit.module).In my environment I have a file field with unlimited files inside and for each time that I upload and/or remove a file, I receive this message.
Others messages are gone!
Best
Comment #26
joachim commentedArgh, there's possibly a case 4, when the 1st AJAX callback is the filefield. Is that how you're getting that error?
Comment #27
pmichelazzoI really don't know what you mean. Can you explain a little bit more?
Thanks
Comment #28
joachim commentedIf you upload files before using the ref option limit widgets.
Comment #29
pmichelazzoJoachim,
The crazy stuff is: I don't have an option limit widget in this content type. Look the shot.
Comment #30
joachim commentedYou have select lists. They work with this module too.
Comment #31
pmichelazzoOh yes,
I'm using the Field Permissions module to show for some roles just the file field to update (and upload) new documents. So, when a user with a specific role edit the node, he just see the file field (documentos). On the edit screen, when he upload a new file and/or remove a file, he see the notice message.
All of the others fields remain with data and can't be updated for this specific role.
Comment #32
joachim commented> I'm using the Field Permissions module
Argh that's a whole new kettle of fish!
Could you test this patch without that please, and open a new bug report for Field Permissions?
Comment #33
pmichelazzo:-)
Sure, I'm doing it tomorrow morning.
Comment #34
pmichelazzoHello,
I make some tests without the Field Permissions module and the problem still there.
When I select a file to upload in my content type and DON'T click at the upload button, the file is uploaded 100% and I don't receive any kind of message. But, if I try to upload more than one file (select a file, click at the upload button and try to upload another one) I receive the message Notice: Undefined index: storage in reference_option_limit_form_alter() (line 361 of /var/www/sites/all/modules/reference_option_limit/reference_option_limit.module) and another one (An illegal choice has been detected. Please contact the site administrator.) because the content type lost the reference from the field.
Cheers,
Comment #35
joachim commentedIt's all working for me now, even with a multi-value filed field.
Could you post the *exact* steps for reproducing the problem, *with* the patch applied.
Comment #36
HakS commentedHello
I think I discovered the problem (I didn't have much time enough to make more tests with this change), When first case is true, you don't have anything stored in $form_state, so when you make an AJAX request which is not from reference option limit (upload an image or remove it), case 3 will execute, and $form_state['reference_option_limit']['storage'] has never been set before. I fixed this putting this line at the end of the first case ("Case 1: First time round."):
This new line removes error message and let you save any change you made with no "Illegal choice detected" message.
I'm new in Drupal, so I still need to learn how to make a patch, besides, I prefer first to test everything before submit any patch.
PD: Sorry if I my English is not good.
Comment #37
gkom commentedThat fixed it, HakS!
I have tested this on different content types with multiple image fields and it works nicely.
For others reading this: the patch in #24 needs to be applied to 7.x-1.3 before adding the line HakS suggested.
joachim, please review this and let us know if you'll be pushing this into -dev.
Thank you all for your help.
Comment #38
joachim commentedI still can't reproduce the problem with the original patch. Could someone give me the clear steps to reproduce this please?
Comment #39
HakS commentedjoachim,
Right now I don't have all resources to exactly reproduce this problem, however, I'll try to give you some clues.
All this happens before adding the line I suggested:
If you still cannot reproduce it even with these clues, I suggest to show us what do you have stored in $form_state['reference_option_limit']['storage'][$field_name]['match_values'] when 1st case runs.
Comment #40
joachim commentedLooks like #2006798: Errors in the form with other ajax elements has this covered.
Comment #41
joachim commentedI'm tentatively going to close this.