Setting the “Allowed Values Function” Property on Text List Fields

Public

Use a update function to set the hidden field setting

</> CopyGet raw version
php
  1. /**
  2.  * Set the my_module_default_sports_list() function as the source of the 'default_sports' field
  3.  */
  4. function my_module_update_7099() {
  5. if (!db_table_exists('field_data_field_default_sports')) {
  6. $out = array(
  7. '#abort' => array(
  8. 'success' => FALSE,
  9. 'query' => 'The field table doesn\'t exist yet.',
  10. ),
  11. );
  12. return $out;
  13. }
  14.  
  15. // change machine name of field
  16. $default_sports_field = field_info_field('field_default_sports');
  17.  
  18. // having this set interferes with the allowed_values_function value
  19. unset($default_sports_field['settings']['allowed_values']);
  20.  
  21. // function name that provides array of values
  22. $default_sports_field['settings']['allowed_values_function'] = 'my_module_default_sports_list';
  23.  
  24. // save
  25. field_update_field($default_sports_field);
  26. }

An example implementation

</> CopyGet raw version
php
  1. /**
  2.  * Fetch list of sports names
  3.  */
  4. function my_module_default_sports_list() {
  5. return array(
  6. 'baseball' => t('Baseball'),
  7. 'bowling' => t('Bowling'),
  8. 'crosscountry' => t('Cross Country'),
  9. . . .
  10. 'volleyball' => t('Volleyball'),
  11. 'waterpolo' => t('Water Polo'),
  12. 'wresting' => t('Wrestling'),
  13. );
  14. }