This can be accomplished by modifying the #after_build item of your term_reference_tree field in hook_form_alter:
function MYMODULE_form_alter(&$form, &$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, &$form_state) {
MYMODULE_taxonomy_tree_form_element_alter($form_element[0]);
return $form_element;
}
function MYMODULE_taxonomy_tree_form_element_alter(&$element) {
foreach ($element as $tid => $data) {
if (($tid[0] != '#') && 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(&$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);
}