Fix for Filter nodes by a taxonomy_content field with view argument accepting terms names and synonyms

  1. /**
  2.  * The following code is used as validator for a view argument to filter nodes having the passed
  3.  * term as value for a taxonomy_content field. The argument can be the term name or one of
  4.  * its synonyms. It works, but the title of the page is the returned tid and not the term's name.
  5.  */
  6. $terms = taxonomy_get_term_by_name($argument);
  7. $synonym_root = taxonomy_get_synonym_root($argument);
  8. if($synonym_root) $terms[] = $synonym_root;
  9. foreach($terms as $term) {
  10.   $parents = taxonomy_get_parents_all($term->tid);
  11.   foreach($parents as $parent) {
  12.     if($parent->name == REQUIRED_PARENT_NAME) {
  13.       $handler->argument = $term->tid;
  14.       return TRUE;
  15.     }
  16.   }
  17. }
  18. return FALSE;
  19.  
  20. /**
  21.  * Since the view is exported in a custom module created with the features module (see http://drupal.org/project/features),
  22.  * the following preprocess_page function is used to overrides the title (and head_title).
  23.  */
  24. function MYMODULE_preprocess_page(&$vars) {
  25.   if(arg(0) == VIEW_PATH && arg(1) && is_numeric($vars['title'])) {
  26.     $tid = (int)$vars['title'];
  27.     $term = taxonomy_get_term($tid);
  28.     if($term) {
  29.       $vars['title'] = check_plain($term->name);
  30.       $vars['head_title'] = str_replace($tid, check_plain($term->name), $vars['head_title']);
  31.     }
  32.   }
  33. }
  34.  
  35. /**
  36.  * The module also needs to set it weight to be greater than views' one (10) so its preprocess_page is called after views' one.
  37.  */
  38. function MYMODULE_install() {
  39.   db_query("UPDATE {system} SET weight = 11 WHERE name = 'MYMODULE'");
  40. }

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.