Hi there,

I am using the term_reference_tree to display a relatively big list of items (country name) divided by continent. The term_reference_tree provides a nice compact widget, and I like it very much.

Now, during the registration process, I want to restrict the list of countries to about 10 items (full list is still available after the registration). Usually I would simply use hook_form_alter and remove the unwanted items from the '#options' item of the taxonomy term field. But term_reference_tree does not use the '#options' mecanism, instead it read directly the terms from the DB to build the tree.

My question: what is the best way to alter/filter the tree before it is being rendered? is there a hook I could use?

Cheers,

S.

Comments

drupal.ninja03’s picture

Hi,

I too am in dire need for the solution to this. Please ca someone point me in the right direction?
Thanks.

hillmark’s picture

You could achieve this by using a view to return a filtered list. Not tried with a hierarchical taxonomy, but I added a field to my taxonomy, which in your case would be a 'visible at registration' Boolean. You can then build a view and filter it by your new field to limit the options returned. The setup for filtering the taxonomy using this view is straightforward after that.

Raghav1981’s picture

Issue summary: View changes

http://drupal.stackexchange.com/questions/100225/programmatically-alter-...
this link will help you on how to alter/filter the tree.

napche’s picture

StatusFileSize
new547 bytes

I added a patch which implements an alter hook right before rendering the tree.
In a recent project I used this to strip some values from the tree.

In your module, just use a my_module_ term_reference_get_children_alter($terms) function.

alex_optim’s picture

Status: Active » Needs review
pifagor’s picture

Status: Needs review » Reviewed & tested by the community

Look good

alex_optim’s picture

Update patch.

alex_optim’s picture

StatusFileSize
new363 bytes
alex_optim’s picture

StatusFileSize
new374 bytes

Update patch.

alex_optim’s picture

Version: 7.x-1.9 » 7.x-1.x-dev
Status: Reviewed & tested by the community » Needs review
StatusFileSize
new462 bytes
pifagor’s picture

Status: Needs review » Needs work

In my opinion, we can use drupal_alter('term_reference_get_children', $tree[$vid]); instead of drupal_alter('term_reference_get_children', $tree[$vid]['terms']);

alex_optim’s picture

Component: Documentation » Code
Status: Needs work » Reviewed & tested by the community
StatusFileSize
new346 bytes

  • alex_optim committed e16d048 on 7.x-1.x
    Issue #1539238 by alex_optim, Napche, pifagor: altering the tree before...
alex_optim’s picture

Status: Reviewed & tested by the community » Patch (to be ported)
alex_optim’s picture

Status: Patch (to be ported) » Fixed
alex_optim’s picture

Status: Fixed » Closed (fixed)