Problem
Throwing Notice: Undefined index: in locale_field_entity_form_submit() (line 438 of /path/to/drupal/installation/modules/locale/locale.module).
Description
I have a content type lets say 'news' with a multiple fields, out of which few are required and few are optional. I have a requirement to have different set of fields to different set of user role. Which i achieved with the help of hook_form_alter() and unset().
On dev instance, locale is enabled and if user is on node/add/news, a content form will appear w/ or w/o some fields, after filling up the form and upon clicking Save button getting following notice per unset field
Undefined index: in locale_field_entity_form_submit() (line 438 of /path/to/drupal/installation/modules/locale/locale.module).
Why I used unset() over hide() or '#access' => FALSE?
- unset() will remove the field structure to process by drupal_render(), keeps field from processing.
- hide() function will hide an field from later rendering, but it this field got processed. :(
- Read about #access
| Comment | File | Size | Author |
|---|---|---|---|
| #1 | throwing_notice-2356283-1.patch | 1.43 KB | rahul.shinde |
Comments
Comment #1
rahul.shindeHere I am submitting patch
Comment #2
rahul.shindeComment #3
sch2 commentedThanks for your time, #1 worked nicely for me.
Comment #4
bojan_dev commentedI have tested patch #1, and works fine for me.
Comment #5
socialnicheguru commentedI had the same problem.
This seems to have fixed it.
Comment #7
rahul.shindeComment #9
rahul.shindeComment #10
tstoecklerSorry, but I don't think we should support modules randomly unsetting form keys. For exactly that reason it is possible to simply set
'#access' => TRUEon the element. That has the same effect (the element is not shown) but avoids issues like this.Let's at least have some more thoughts on this. For me personally this is a won't fix, sorry :-/
Comment #11
tstoecklerOh also it should be proven that this issue does not exist in Drupal 8, otherwise it would need to be fixed there first. Those are the rules, I'm afraid... :-/
Comment #12
eMuse_be commentedI am having an issue with a multi value text area field.
The form field does exist, but the entity does not contain the field. This breaks line 444 $entity->{$field_name}[$previous_language]
Comment #13
eMuse_be commentedthis did the trick for me:
if (!empty($form[$field_name]) && !empty($entity->{$field_name})) {
Comment #14
mgiffordUnassigning @rahul.shinde in the hopes of determining if this is just a D7 issue or if it needs to be fixed in D8 first.
Comment #15
kladrian commentedI have same issue here, don't know if strictly related to module entityfield_prepopulate but I have field removed from $form, I've wrote a patch that looks exactly like #1 and it works for me.
thanks.
Comment #16
Leeteq commentedOk, so we need to verify if this is a 8.x issue as well. Changing queue.
Comment #17
Leeteq commentedComment #18
david_garcia commentedThe issue is that you are using unset() which should de a total don't.
There are numerous cases (not only the one reported here) where using unset() on a form will have unpredictable consequences. Those fields might have been put there by other modules, and they expect them to be there when processing the form.
The way to hide a form element is to use #access = FALSE.
To me this is a "Closed: works as designed", but someone else feel free to do that.
Comment #19
david_garcia commentedAnd of course, if this ever happens to really need a fix, it must be fixed in D8 first - or at least decide if the bug is there.
Comment #20
maximpodorov commentedThe patch for D7 in #1 works perfectly. Please commit.
Comment #21
maximpodorov commentedComment #23
dbielke1986 commentedThe patch for D7 in #1 works perfectly. Please commit.
Comment #24
damienmckennaThe tags were unnecessary. And this is the wrong way to fix an end-user coding mistake.
Marking it "won't fix" in lieu of a status that just says "don't break your site".
Comment #25
maximpodorov commentedI think it's not a mistake to create a form without all the fields of an entity. Could this patch be accepted for D7?
Comment #26
David_Rothstein commentedLooks to me like locale_field_entity_form_submit() is only called during submit of the full node or comment form.
So I don't see how this could be reproduced except as noted above... code which does unset() on existing form fields rather than setting #access to FALSE. As others have said above that's not a supported or recommended way to alter a form.
Is there some other way to reproduce this without the unset()?
Comment #27
m-webtechnik commenteduse support module, create a new support ticket, leave body field empty results in:
Notice: Undefined index: body in locale_field_entity_form_submit() (Zeile 438 von /var/www/.../htdocs/modules/locale/locale.module).
Comment #28
dunebl#1 solve my problem! Many thanks.