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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

davidwbarratt’s picture

Issue summary: View changes

Fixed typo, missed a single quote.

davidwbarratt’s picture

Priority: Normal » Major
davidwbarratt’s picture

Version: 7.10 » 7.x-dev
Priority: Major » Normal
jazzdrive3’s picture

I'm getting this same error for the same reason. My error is giving it on line 3648. Using 7.12.

Shawn DeArmond’s picture

Status: Active » Needs review
FileSize
651 bytes

This seems to occur when, as you say, the field is locked.

Here's a patch that worked for me.

Status: Needs review » Needs work

The last submitted patch, locked_user_fields_error-1392852-4.patch, failed testing.

Shawn DeArmond’s picture

Doh! updated.

Shawn DeArmond’s picture

Status: Needs work » Needs review

Go, Bot, go!

bulldozer2003’s picture

Title: Notice: Undefined index: instance in user_form_field_ui_field_edit_form_alter() (line 3644 of /modules/user/user.module). » User registration form field displays when user field is locked accompanied by error message.
Issue tags: +locked, +user fields, +field_create_instance
FileSize
503 bytes

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

FreekVR’s picture

Status: Needs review » Reviewed & tested by the community

Patch in #6 looks good and works for me. The correct 'This field cannot be editted' text now appears and no more errors ;)

David_Rothstein’s picture

Status: Reviewed & tested by the community » Fixed

Committed #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.

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

Fixed a Typo.