I am using the References module to create a User Reference programmatically. However, when I use the field that is created, there is no listing of the users to choose from. The User Reference works when I create a field from the UI. I have attempted to use autocomplete widget (as seen below) and select widget. Neither populates the field with users.

I first create the field as follows in my hook_install function:

    $field = array(
        'field_name' => 'vendor_user_reference',
        'type' => 'user_reference',
        'cardinality' => 1,
        'entity_types' => array('commerce_product'),
        'translatable' => FALSE,
        'locked' => TRUE,
    );
    field_create_field($field);

I then call when the product type is created the following code

$instance = field_info_instance('commerce_product', 'vendor_user_reference', $product_type['type']);

if (empty($instance)) {
    $new_instance = array(
            'field_name' => 'vendor_user_reference',
            'entity_type' => 'commerce_product',
            'bundle' => $product_type['type'],

            'label' => t('Vendor User Reference'),
            'required' => TRUE,
            'settings' => array(),
            'widget' => array(
                'type' => 'user_reference_autocomplete',
                'weight' => -10,
                'settings' => array(
                        'autocomplete_match' => 'contains',
                        'size' => 20,
                        'autocomplete_path' => 'user_reference/autocomplete',
                ), //end setting array
            ), //end widget array   
            'display' => array(),
    );
    field_create_instance($new_instance);

Comments

angms-bh’s picture

I got it to work if I added at least a referenceable_status array in the field creation. By default, the user_reference type defines the value in the setting array as array(). If you do that, then the field widget doesn't know what to populate the list with. So, I added at least active users in the field definition by defining the 'referenceable_status array as below:

$field = array(
	'field_name' => 'vendor_user_reference',
	'type' => 'user_reference',
	'cardinality' => 1,
	'entity_types' => array('commerce_product'),
	'translatable' => FALSE,
        'settings' => array(
              	'referenceable_status' => array(
		     0 => 0,
		     1 => '1',
	        ),  
        )       
    );

	
field_create_field($field);

I put this in my hook_install.

I then created an instance as follows:

		$new_instance = array(
				'field_name' => 'vendor_user_reference',
				'entity_type' => 'commerce_product',
				'bundle' => $product_type['type'],
				'label' => t('Vendor User Reference'),
				'required' => TRUE,
				'widget' => array(
						'active' => 1,
						'module' => 'options',
						'settings' => array(
								'apply_chosen' => '',
						),
						'type' => 'options_select',
						'weight' => '40',
				),

				'display' => array(),
		);
		field_create_instance($new_instance);

I think in reality, the User Reference Module should define at least something in the refereanceable_status array when defining the field in user_reference_field_info().