1

Say the user is creating a node. I want to display a drop down option list, with prepopulated options, based on a calculation (i.e. reference to user's groups, user's other nodes, etc...). Drupal 7.

I've had no luck with the rules, computed field, prepopulate field modules... maybe i'm missing something?

1

Use hook_form_alter or hook_form_FORM_ID_alter to pre-populate your dropdown list with results returned from a custom function.


For example, using a form of hook_form_alter():

$form['field_my_field']['und']['#options'] = _my_custom_function();

Then make sure your function _my_custom_function() return's a proper array.

function _my_custom_function() {
    $custom_values = array();
    $custom_values['key1'] = 'value1';
    $custom_values['key2'] = 'value2';
    $custom_values['key3'] = 'value3';
    return $custom_values;
}

Obviously you can include some 'logic' in the _my_custom_function() to return your calculated list.


Here are a few super-useful link's I wish I would have know about before.

| improve this answer | |
0

write a module and provide a hook_form_alter to change the values of the form element. Just google hook_form_alter for tutorials and code snippets.

| improve this answer | |
0

Are you using a list field?

It has support for a callback to provide the allowed values. See list_field_info() That is however not exposed in the UI (which makes somewhat sense as you can't implement a function there either).

So you can simply implement a function that returns the allowed values and then update the field configuration (through api calls in your module, directly in the database, or export your field as a feature and then adjust it there).

In fact, if it is a list field, you probably have to do it this way instead of a hook_form_alter() in order to get past the validation, but I'm not 100% sure.

| improve this answer | |

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.