0

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 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);
}

This creates the field on the entity/bundle, but when I try to use it from the UI, there are no user listings. This is the case if I use select list or autocomplete.

What am I doing wrong?

0

Programmatically create user reference field

I have found the solution to having the user reference field populate the user option list programmatically. You need to initialize the referenceable_status settings when defining the user reference field. See my answer in the Question above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.