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

CommentFileSizeAuthor
#1 throwing_notice-2356283-1.patch1.43 KBrahul.shinde

Comments

rahul.shinde’s picture

StatusFileSize
new1.43 KB

Here I am submitting patch

rahul.shinde’s picture

Status: Active » Needs review
sch2’s picture

Thanks for your time, #1 worked nicely for me.

bojan_dev’s picture

Status: Needs review » Reviewed & tested by the community

I have tested patch #1, and works fine for me.

socialnicheguru’s picture

I had the same problem.
This seems to have fixed it.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 1: throwing_notice-2356283-1.patch, failed testing.

rahul.shinde’s picture

Assigned: Unassigned » rahul.shinde

Status: Needs work » Needs review
rahul.shinde’s picture

Status: Needs review » Reviewed & tested by the community
tstoeckler’s picture

Status: Reviewed & tested by the community » Needs review

Sorry, but I don't think we should support modules randomly unsetting form keys. For exactly that reason it is possible to simply set '#access' => TRUE on 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 :-/

tstoeckler’s picture

Oh 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... :-/

eMuse_be’s picture

I 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]

eMuse_be’s picture

this did the trick for me:

if (!empty($form[$field_name]) && !empty($entity->{$field_name})) {

mgifford’s picture

Assigned: rahul.shinde » Unassigned

Unassigning @rahul.shinde in the hopes of determining if this is just a D7 issue or if it needs to be fixed in D8 first.

kladrian’s picture

I 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.

Leeteq’s picture

Version: 7.31 » 8.0.x-dev

Ok, so we need to verify if this is a 8.x issue as well. Changing queue.

david_garcia’s picture

The 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.

david_garcia’s picture

Status: Needs review » Needs work

And 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.

maximpodorov’s picture

The patch for D7 in #1 works perfectly. Please commit.

maximpodorov’s picture

Title: Throwing Notice: Undefined index: <field_name> in locale_field_entity_form_submit() (line 438 of /path/to/drupal/installation/modules/locale/locale.module). » Notice: Undefined index: <field_name> in locale_field_entity_form_submit() (line 438 of modules/locale/locale.module).

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dbielke1986’s picture

The patch for D7 in #1 works perfectly. Please commit.

damienmckenna’s picture

Status: Needs work » Closed (won't fix)
Issue tags: -#form, -#locale, -node_form, -submit, -node_validate, -hide(), -#access

The 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".

maximpodorov’s picture

Version: 8.1.x-dev » 7.x-dev
Category: Feature request » Bug report
Status: Closed (won't fix) » Needs work

I think it's not a mistake to create a form without all the fields of an entity. Could this patch be accepted for D7?

David_Rothstein’s picture

Status: Needs work » Postponed (maintainer needs more info)

Looks 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()?

m-webtechnik’s picture

Is there some other way to reproduce this without the unset()?

use 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).

dunebl’s picture

#1 solve my problem! Many thanks.

Status: Postponed (maintainer needs more info) » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.