Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I received the following error when I went to http://example.com/admin/config/people/accounts/fields/example_field
Notice: Undefined index: instance in user_form_field_ui_field_edit_form_alter() (line 3644 of /modules/user/user.module).
The reason I received this error was because I pragmatically created a field for a user where locked is TRUE
/*
* Returns the fields
*/
function example_fields() {
return array(
array(
'field_name' => 'example_field',
'type' => 'text',
'locked' => TRUE,
'settings' => array(
'no_ui' => TRUE,
'max_length' => 100,
),
),
);
}
/*
* Returns the field instances
*/
function example_instances() {
return array(
array(
'field_name' => 'example_field',
'entity_type' => 'user',
'bundle' => 'user',
'label' => 'Example Field',
'widget' => array(
'settings' => array(
'size' => 100,
),
),
),
);
}
/**
* Implements hook_install().
*/
function example_install() {
// Create the subscriber fields
foreach (example_fields() as $field) {
field_create_field($field);
}
foreach (example_instances() as $instance) {
field_create_instance($instance);
}
}
/**
* Implements hook_uninstall().
*/
function example_uninstall() {
foreach (example_instances() as $instance) {
field_delete_instance($instance);
}
}
If I change locked to FALSE then I stop receiving the error.
Comment | File | Size | Author |
---|---|---|---|
#8 | drupal_locked_user_fields_1392852_8.patch | 503 bytes | bulldozer2003 |
#6 | locked_user_fields_error-1392852-6.patch | 641 bytes | Shawn DeArmond |
#4 | locked_user_fields_error-1392852-4.patch | 651 bytes | Shawn DeArmond |
Comments
Comment #0.0
davidwbarratt CreditAttribution: davidwbarratt commentedFixed typo, missed a single quote.
Comment #1
davidwbarratt CreditAttribution: davidwbarratt commentedComment #2
davidwbarratt CreditAttribution: davidwbarratt commentedComment #3
jazzdrive3 CreditAttribution: jazzdrive3 commentedI'm getting this same error for the same reason. My error is giving it on line 3648. Using 7.12.
Comment #4
Shawn DeArmond CreditAttribution: Shawn DeArmond commentedThis seems to occur when, as you say, the field is locked.
Here's a patch that worked for me.
Comment #6
Shawn DeArmond CreditAttribution: Shawn DeArmond commentedDoh! updated.
Comment #7
Shawn DeArmond CreditAttribution: Shawn DeArmond commentedGo, Bot, go!
Comment #8
bulldozer2003My patch looks for the locked setting and returns before the function does anything else. Displaying the registration form field checkbox is fruitless since you cannot submit the setting.
When adding required locked fields to user entities programattically, you need to be sure to set $instance['settings']['user_register_form'] to TRUE as well! Otherwise the field will not be displayed on the registration form nor will you be able to set the option through the UI.
Comment #9
FreekVR CreditAttribution: FreekVR commentedPatch in #6 looks good and works for me. The correct 'This field cannot be editted' text now appears and no more errors ;)
Comment #10
David_Rothstein CreditAttribution: David_Rothstein commentedCommitted #6 to 7.x - thanks! http://drupalcode.org/project/drupal.git/commit/922166f
(#8 looks like it would have been good too, but I went with #6 since that's the one that got some manual testing by @FreekVR.)
Note: This doesn't seem to be an issue in Drupal 8.
Comment #11.0
(not verified) CreditAttribution: commentedFixed a Typo.