Drupal allowed_values_function does not get called when creating a field

1.2k views Asked by At

For some reason my allowed_values_function never gets called when showing a field on a user bundle. Code:

function get_business_units()
{
    $options = entity_load('business_unit', FALSE, NULL, FALSE);
    $opt = bu_to_list_values($options);
    return $opt;
}


function MYMODULE_enable()
{
    if (!field_info_field('field_user_business_unit')) {
        $field = array(
            'field_name' => 'field_user_business_unit', 
            'type' => 'text', 
            'settings' => array(
                'allowed_values' => array(),
                'allowed_values_function' => 'get_business_units',
            )
        );
        field_create_field($field);

        // Create the instance on the bundle.
        $instance = array(
            'field_name' => 'field_user_business_unit', 
            'entity_type' => 'user', 
            'label' => 'Business Unit', 
            'bundle' => 'user', 
            'required' => FALSE,
            'settings' => array(
                'user_register_form' => 1,
        ),
            'widget' => array(
                'type' => 'options_select',
        ),
        );
        field_create_instance($instance);
    }
}

The field is created, and even displayed on the users "edit" page when editing their info. But the only value is "Select" or "None". My method is never called (I even placed a debug point). This is all in MYMODULE.install file.

3

There are 3 answers

0
user4924448 On

The problem is: 'type' => 'text'.

You have to use: 'type' => 'list_text'.

Allowed values is meaningless for a text type.

2
Clive On

Your get_business_units() function needs to be in the MYMODULE.module file; the .install files aren't included in a normal Drupal bootstrap.

0
ARA1307 On

Have you tried drush features-revert MYMODULE ?