Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
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
Comment #1
angms-bh CreditAttribution: angms-bh commentedI 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:
I put this in my hook_install.
I then created an instance as follows:
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().