1

I want to add a user reference field in taxonomy term. I create field and it's instance:

$field = ['field_name' => 'mymodule_employee',
        'label' => t('Employee'),
        'type' => 'entityreference',
        'cardinality' => 1,
        'settings' => [
            'target_type' => 'user',
            'handler' => 'base',
            'handler_settings' => [
                'target_bundles' => ['user']
            ],
        ],
];
field_create_field($field);

$instance = ['mymodule_employee' => [
            'entity_type' => 'taxonomy_term',
            'bundle' => 'my_vocabulary',
            'field_name' => 'mymodule_employee',
            'label' => t('Employee'),
            'widget' => array(
                'type' => 'options_select',
                'active' => 1,
            ),
            'settings' => array(
                'target_type' => 'user',
                'handler_settings' => array('target_bundles' => null),
                'handler' => 'base',
            ),
            'display' => array(
                'default' => array('label' => 'inline', 'type' => 'entityreference_label'),
            ),
            'required' => 0,
            'default_value' => null,
        ],
];
field_create_instance($instance);

Everything goes ok. But when I'm creating the term and try to choose a user there is an empty list. When I go to the field settings in admin form and just save it, everything begins to work.

What do I forget creating field and instance?

2
  • Would just using Entity reference be an option?
    – Felix Eve
    Commented Mar 31, 2014 at 10:49
  • It works with this module if User Reference is set in admin form. It does not work if is is set programmatically.
    – menelai
    Commented Mar 31, 2014 at 12:34

2 Answers 2

1

Check if Entity Reference Autocomplete module helps you. The module page says

This module defines a new form element type, called "entityreference", that allows developers to add autocomplete fields to forms, so that users can reference any entity in the same way they would do through an Entity Reference field.

1

To add a reference to the user, use the References module, and then enable the User Reference module defined by it.

Then you will need to define the field in your hook_enable or hook_install function. Do something like the following:

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

You MUST define a value for referenceable_status. If you don't, the user list will not populate in the interface. You can also at this time define the referenceable_roles array too. It would look something like this: 'referenceable_roles' => array( 2 => 0, 3 => 0, 4 => '4', 5 => 0, 6 => 0, ),

where you set the role you want to the rid from the role table.

You will then need to create an instance of the field on your entity. I did the following for a Drupal Commerce Product. (for Commerce Kickstart 2.0, this goes in the hook_commerce_product_type_insert($product_type, $skip_reset) for a product type entity.)

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

Now, if you want to set the value of the select list that is created for the users (say the logged in user), You will need to modify your hook_form_alter function to add the following:

$entity_form['vendor_user_reference']['und']['#default_value'] = (string)$user->uid;

I am not quite sure why I need the ['und'] for my form, but it wouldn't work without it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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