10

I have created a select list for a Profile2 account in Drupal 7. In the allowed values list, I have added some values. Now what I would like to do is programmatically update the values in that list (add, delete or update). I don't want to use form_alter as I want these updates committed to the database. My problem is I can't seem to find where these values are stored in the database or how to pull those values from Profile2, update them and then save them.

I have Googled high and low on this particular issue and can't find an answer on how to do this. I hope someone can help me out.

Thanks.

2 Answers 2

23

Something along the lines of...

// Get the field info
$info = field_info_field('field_some_field');

// Get a reference to the values
$values = &$info['settings']['allowed_values'];

// Manipulate the values in some way, e.g.
$values['new_value'] = 'New Value';

// Save the field
field_update_field($info);
4
  • where I need to add this piece of code?
    – Shivam
    Sep 12, 2015 at 5:27
  • @shivam You need to add this code to a custom module that you create.
    – user5013
    Jan 29, 2016 at 19:20
  • Put this in a hook_update_N to run just one time. Nov 20, 2019 at 15:23
  • Also this won't change old allowed values if the field is already in use. Nov 20, 2019 at 15:51
3

You'd better use the allowed_values_function with a callback:

'settings' => array(
  'allowed_values' => array(),
  'allowed_values_function' => '_my_feature_module_options_list',
),

See Creating dynamic allowed values options for an entity field.

1
  • A more complete answer would be helpful. The code you're referencing is good, and works similar to the accepted answer, but was created for use with the Features module. Dec 8, 2017 at 18:30

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.