9

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.

22

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);
  • where I need to add this piece of code? – Shivam Sep 12 '15 at 5:27
  • @shivam You need to add this code to a custom module that you create. – user5013 Jan 29 '16 at 19:20
2

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.

  • 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. – Johnathan Elmore Dec 8 '17 at 18:30

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.