Jump to:

As outlined in that issue, there may be a need for filtering the retrieval of $term (name) from $vocabularies array($A, $B, $C). The suggested solution is to invoke a loop repeatedly calling taxonomy_get_term_by_name() with the $vocabulary constraint set to $A or $B or $C. Within taxonomy_get_term_by_name() is a call to taxonomy_vocabulary_get_names(), which queries the data base for defined $vocabularies and the returns the resulting array.

To make taxonomy_vocabulary_get_names() more performant for a particular page request, the return value should be stored in a static array to minimize the data base query. An open issue is whether the function profile should be changed to include a parameter $reset = FALSE.

Thanks @karschap for your initial stab. I note that you are attempting to change taxonomy_get_term_by_name(). That function is being worked on in #336697: Optional vid argument for taxonomy_get_term_by_name(). The purpose of this issue is to add a static to taxonomy_vocabulary_get_names() which will be called by that modified taxonomy_get_term_by_name().

On your patch attempt, there a couple of other things that would need to be changed. First, you need set $term before returning; otherwise the static storage has no effect. Second, Drupal coding standards call for formatting else statements like:

Patch in #8 is a step in the right direction but still have some issues:
- indentation is incorrect (Drupal coding standards use 2 spaces)
- drupal_static() is called with a default value, so $names will always be set and the underlying conditional block of code will never be run

added static reset function call for "taxonomy_vocabulary_get_names" (in analogy to taxonomy_terms_static_reset() ) in taxonomy_vocabulary_save($vocabulary) and taxonomy_vocabulary_delete($vid) functions to have a cache reset after changing vocabularues.

taxonomy_vocabulary_get_names() is a hack because we're using the vocabulary (which is an entity) as a bundle, and you get infinite recursion in hook_entity_info() if you don't have the workaround function.

However while I'd like to see us change vocabularies to configuration in Drupal 8, while they're entities we need to encourage use of this function when you only need a list of all bundle names or mapping vid to name, so adding the static cache here makes sense.

Patch failures in here show it is already tested and the static reset is consistent with terms (which also have meta functions on top of loading).

So I've committed this to 8.x, moving back to 7.x for webchick to consider since there's the same issue there.