Programmatically alter taxonomy term tree options I have a taxonomy term reference field in a form. This field has many options and I'm using the term_reference_tree module to display them.
I'm trying to add a filter to this form so it only displays certain options depending on some factors (the value of a different field).

The problem is editing this field's options. I can't find a way to programmatically alter a taxonomy term reference field's options when using the term reference tree widget.
I've tried using hook_field_widget_properties_ENTITY_TYPE_alter but this doesn't allow me to alter the actual options.

Any suggestions would be very welcome. Thanks in advance.
This can be accomplished by modifying the #after_build item of your term_reference_tree field in hook_form_alter:

function MYMODULE_form_alter(&amp;$form, &amp;$form_state) {
  // You might want to do this only for certain forms,
  // or only for forms that have the field to be altered.
  $language = $form['my_taxonomy_field']['#language'];
  $form['my_taxonomy_field'][$language]['#after_build'][] = 'MYMODULE_branch_group_form_element_after_build';
}

function MYMODULE_branch_group_form_element_after_build($form_element, &amp;$form_state) {
  MYMODULE_taxonomy_tree_form_element_alter($form_element[0]);
  return $form_element;
}

function MYMODULE_taxonomy_tree_form_element_alter(&amp;$element) {
  foreach ($element as $tid =&gt; $data) {
    if (($tid[0] != '#') &amp;&amp; is_numeric($tid)) {
      // MODIFY HERE:  If you do not want to show the term with
      // a specified tid, hide() it here.
      if ($tid == 60) {
        hide($element[$tid]);
      }
      else {
        // OPTIONAL: Alter the way each individual term is rendered.
        // See BONUS ANSWER, below.
        MYMODULE_taxonomy_tree_form_element_term_alter($element[$tid][$tid], $tid);
        // Recursively alter the children of the tree -- the item we
        // want to modify may be nested somewhere inside the tree.
        if (array_key_exists($tid . '-children', $element[$tid])) {
          MYMODULE_taxonomy_tree_form_element_alter($element[$tid][$tid . '-children']);
        }
      }
    }
  }
}


BONUS ANSWER:  In addition to filtering out items, I also wished to add a rendered icon with each taxonomy term in the tree.  In order to allow the specific fields of the taxonomy vocabulary to be selected and ordered, and the rendering mode (i.e. thumbnail size) of the image field to be selected through the admin user interface, I used the entity_view_mode module to define a new view mode for my taxonomies.

/**
 * Alter the data built by term_reference_tree.widget.inc.
 * @see: _term_reference_tree_build_item()
 */
function MYMODULE_taxonomy_tree_form_element_term_alter(&amp;$element, $tid) {
  $term = taxonomy_term_load($tid);
  // In order for this to work, you will need to install the
  // entity_view_mode module, and make a 'term_with_icon'
  // view mode for your taxonomy vocabulary.
  $term_render_array = taxonomy_term_view($term, 'term_with_icon');
  $element['#title'] = drupal_render($term_render_array);
}