Index: view_alias.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/view_alias/view_alias.module,v
retrieving revision 1.4
diff -u -r1.4 view_alias.module
--- view_alias.module	3 Dec 2009 15:51:56 -0000	1.4
+++ view_alias.module	6 Dec 2009 21:44:55 -0000
@@ -38,21 +38,17 @@
  *
  */
 function view_alias_pathauto_bulkupdate() {
-  
   $aliasable = _get_aliasable_displays();
-  
-  foreach($aliasable as $alias) {
-    if(variable_get("pathauto_view_alias_{$alias->path}", FALSE)) {
+
+  foreach ($aliasable as $alias) {
+    if (variable_get("pathauto_view_alias_{$alias->path}", FALSE)) {
       $terms = taxonomy_get_tree($alias->varg);
 
-      foreach($terms as $term) {
-        $clean_term_name = pathauto_cleanstring($term->name);
-        path_set_alias("$alias->path/$term->tid","$alias->path/$clean_term_name"); 
-        drupal_set_message("Alias $alias->path/$term->tid to $alias->path/$clean_term_name");
+      foreach ($terms as $term) {
+        view_alias_create_alias($term, $alias, 'bulkupdate');
       }
     }
   }
-
 }
 
 /**
@@ -101,6 +97,58 @@
   }
 }
 
+/**
+ * Implementation of hook_taxonomy().
+ */
+function view_alias_taxonomy($op, $type, $array = NULL) {
+  if ($type == 'term') {
+    $term = (object) $array;
+    $aliasable = _get_aliasable_displays();
+    foreach ($aliasable as $alias) {
+      if ($alias->varg == $term->vid) {
+        if ($op == 'delete') {
+          view_alias_delete_alias($term, $alias);
+        }
+        else {
+          view_alias_create_alias($term, $alias, $op);
+        }
+      }
+    }
+  }
+}
+
+/**
+ * Given a term, generate its view aliases.
+ */
+function view_alias_create_alias($term, $alias, $op) {
+  module_load_include('inc', 'pathauto');
+  if (strpos($alias->path, '%') === FALSE) {
+    $source = "$alias->path/$term->tid";
+  }
+  else {
+    $source = str_replace('%', $term->tid, $alias->path);
+  }
+
+  // Pathauto uses variable_get() to retrieve patterns. Since we need multiple
+  // patterns for every path, we temporarily override the variable
+  // "pathauto_view_alias_pattern" which Pathauto will check for this operation.
+  $GLOBALS['conf']['pathauto_view_alias_pattern'] = $alias->path;
+  $replacements = array(
+    'tokens' => array('%'),
+    'values' => array(pathauto_cleanstring($term->name)),
+  );
+  $alias_path = pathauto_create_alias('view_alias', $op, $replacements, $source, $term->tid);
+  unset($GLOBALS['conf']['pathauto_view_alias_pattern']);
+  return $alias_path;
+}
+
+/**
+ * Delete an alias set by View Alias.
+ */
+function view_alias_delete_alias($term, $alias) {
+  $alias_path = view_alias_create_alias($term, $alias, 'update');
+  path_set_alias(NULL, $alias_path);
+}
 
 /**
  *
