Customise taxonomy select options I have a vocabulary with a user reference field. In a content type I have a field that references one or more terms from this vocabulary with a select widget. I would like to restrict the options to only the terms that are referencing the logged in user.

Drupal 7, Entityreference module is used for the user reference in the term and the select widget that I want to customize in the node is a Term reference.
EDIT: If you use Features and the field under that references the taxonomy term under question is in one of your features, there's is much easier way to do this. Add this to your my_module.field_base.inc:


// Exported field_base: 'field_user_blogs_ref'
$field_bases['field_user_blogs_ref'] = array(
    ...
    'settings' => array(
      ...
      'options_list_callback' => 'my_callback',
    ),


Neat. This has the additional benefit that it applies only to this field base as there might be more fields referencing this vocabulary. The same could probably be done with the code below as well but extra effort.

ORIGINAL ANSWER: still leaving this as this method might be useful for some cases.

I finally did this by altering the code that provides the options to the select widget. You can see how this is done in the content_taxonomy module

1) Provide a hook_field_info_alter to set the callback function


function content_taxonomy_field_info_alter(&amp;$info) {
    $info['taxonomy_term_reference']['settings']['options_list_callback'] = 'content_taxonomy_allowed_values';
}


2) Provide the callback that returns the options


function content_taxonomy_allowed_values($field) {
  $options = array();
  foreach ($field['settings']['allowed_values'] as $tree) {
    if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
      if ($tree['vocabulary'] == 'user_blogs') {
        return my_callback($vocabulary);
      }
      $max_depth = (isset($tree['depth']) &amp;&amp; !empty($tree['depth'])) ? $tree['depth'] : NULL;
      if ($terms = taxonomy_get_tree($vocabulary->vid, $tree['parent'], $max_depth)) {
        foreach ($terms as $term) {
          $options[$term->tid] = str_repeat('- ', $term->depth) . $term->name;
        }
      }
    }
  }
  return $options;
}


Notice that the function provides the options for all taxonomy vocabularies, but if the vocabulary is of a certain type then it calls another function that returns the options the way required. If you don't use the content_taxonomy module you can copy and use the taxonomy_allowed_values function from the taxonomy module.