? 227947-redirect.patch
? 6-x-path.patch
? 615258-path-source.patch
? test.patch
Index: API.php
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/domain/API.php,v
retrieving revision 1.49
diff -u -p -r1.49 API.php
--- API.php	24 Oct 2009 22:11:26 -0000	1.49
+++ API.php	26 Oct 2009 20:47:51 -0000
@@ -334,6 +334,27 @@ function hook_domainwarnings() {
 }
 
 /**
+ * Allows modules to specify the target link for a node or other Drupal object.
+ *
+ * @param &$source
+ *   The domain array from domain_get_node_match(), passed by reference.
+ * @param $id
+ *   The identifier of the obect being rewritten. For nodes, this is the node id.
+ *   In other instances, we may pass a path or other variable.
+ * @param $type
+ *   The type of lookup being performed. (Defaults to 'node'. This value is here
+ *   so that we can eventually rewrite non-node paths.)
+ * @return
+ *   No return value; modify $source by reference.
+ */
+function hook_domain_source_alter(&$source, $id, $type = 'node') {
+  if ($type == 'node') {
+    // Taken from the Domain Source module
+    $source = domain_source_lookup($nid);
+  }
+}
+
+/**
  * Allows modules to add additional form elements for saving as domain-specific
  * settings.
  *
Index: domain.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/domain/domain.module,v
retrieving revision 1.128
diff -u -p -r1.128 domain.module
--- domain.module	24 Oct 2009 22:11:26 -0000	1.128
+++ domain.module	26 Oct 2009 20:47:54 -0000
@@ -305,10 +305,8 @@ function domain_block($op = 'list', $del
             if (!empty($this_node->subdomains)) {
               $output .= theme('item_list', $this_node->subdomains, t('Assigned domains'));
             }
-            if (isset($this_node->domain_source)) {
-              $this_domain = domain_lookup($this_node->domain_source);
-              $output .= theme('item_list', array($this_domain['sitename']), t('Source domain'));
-            }
+            $this_domain = domain_get_node_match($this_node->nid);
+            $output .= theme('item_list', array($this_domain['sitename']), t('Source domain'));
             if (empty($output)) {
               $output = t('This node is not assigned to a domain.');
             }
@@ -1145,6 +1143,22 @@ function domain_nodeapi(&$node, $op, $a3
 }
 
 /**
+ * Get the best matching domain for a node link.
+ */
+function domain_get_node_match($nid) {
+  static $domain;
+  if (isset($domain[$nid])) {
+    return $domain[$nid];
+  }
+  // Load the domain data for this node -- but only take the first match.
+  $id = db_result(db_query_range("SELECT gid FROM {domain_access} WHERE nid = %d AND realm = '%s' ORDER BY gid", $nid, 'domain_id', 0, 1));
+  $source = domain_lookup($id);
+  drupal_alter('domain_source', $source, $nid);
+  $domain[$nid] = $source;
+  return $source;
+}
+
+/**
  * Get the domains for a node.
  *
  * @param $nid
@@ -1838,24 +1852,24 @@ function domain_invalid_domain_requested
   }
   // Try to find the proper redirect for a node.
   $path = "node/$node->nid";
-  if (isset($node->domain_source)) {
-    $domain = domain_lookup($node->domain_source);
-    if ($domain['valid']) {
-      $redirect = $domain;
-    }
-    else if (!empty($node->domains)) {
-      foreach ($node->domains as $domain_id) {
-        if ($domain_id == -1) {
-          $domain_id = 0;
-        }
-        $domain = domain_lookup($domain_id);
-        if ($domain['valid']) {
-          $redirect = $domain;
-          break;
-        }
+
+  $domain = domain_get_node_match($node->nid);
+  if ($domain['valid']) {
+    $redirect = $domain;
+  }
+  else if (!empty($node->domains)) {
+    foreach ($node->domains as $domain_id) {
+      if ($domain_id == -1) {
+        $domain_id = 0;
+      }
+      $domain = domain_lookup($domain_id);
+      if ($domain['valid']) {
+        $redirect = $domain;
+        break;
       }
     }
   }
+
   // If we found no node matches, just go to the home page.
   $extra = ' '. t('node page.');
   if (empty($redirect)) {
Index: settings_custom_url.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/domain/settings_custom_url.inc,v
retrieving revision 1.24
diff -u -p -r1.24 settings_custom_url.inc
--- settings_custom_url.inc	24 Oct 2009 16:18:52 -0000	1.24
+++ settings_custom_url.inc	26 Oct 2009 20:47:54 -0000
@@ -40,15 +40,21 @@ function domain_url_outbound_alter(&$pat
   }
 
   // Set static variables for the node lookups, to remove redundant queries.
-  static $domain_site, $domain, $nodepaths, $path_rewrite;
+  static $domain_site, $domain, $nodepaths, $path_rewrite, $use_source, $root;
+  static $path_lookup = array();
 
   // This routine only needs to be run from certain urls or if we want to
   // force all links to go to a single domain for SEO.
   // See http://drupal.org/node/195366 for the background.
   $check = domain_grant_all();
   $seo = variable_get('domain_seo', 0);
-  // If using Domain Source, we force links to a specific domain.
-  $use_source = function_exists('domain_source_lookup');
+  if (!isset($use_source)) {
+    $use_source = (bool) count(module_implements('domain_source_alter'));
+  }
+
+  if (!isset($root)) {
+    $root = domain_lookup(variable_get('domain_default_source', 0));
+  }
 
   if ($check || $seo || $use_source) {
     // Check to see if this is a node or comment link and set $nid accordingly.
@@ -76,7 +82,6 @@ function domain_url_outbound_alter(&$pat
     $target_domain_id = $_domain['domain_id'];
     // This path has matched a node id, so it may need to be rewritten.
     if ($nid) {
-      $root = domain_lookup(variable_get('domain_default_source', 0));
       // Remove redundancy from the domain_site check.
       if (!isset($domain_site[$nid])) {
         // If this check works, we don't need to rewrite the path unless SEO rules demand it.
@@ -85,16 +90,7 @@ function domain_url_outbound_alter(&$pat
       if (!$domain_site[$nid] || $use_source) {
         // Remove rendundancy from the domain_id check.
         if (!isset($domain[$nid])) {
-          // The Domain Source module is optional, and allows nodes to be assigned to specific domains for the
-          // purpose of this check.
-          if ($use_source) {
-            $domain[$nid] = domain_source_lookup($nid);
-          }
-          else {
-            // Load the domain data for this node -- but only take the first match.
-            $id = db_result(db_query_range("SELECT gid FROM {domain_access} WHERE nid = %d AND realm = '%s' ORDER BY gid", $nid, 'domain_id', 0, 1));
-            $domain[$nid] = domain_lookup($id);
-          }
+          $domain[$nid] = domain_get_node_match($nid);
         }
         // Can we and do we need to rewrite this path?
         if ($domain[$nid] != -1 && $domain[$nid]['domain_id'] != $_domain['domain_id']) {
@@ -117,6 +113,22 @@ function domain_url_outbound_alter(&$pat
         $target_domain_id = $root['domain_id'];
       }
     }
+    // Hook for non-node paths.
+    else if (!empty($path)) {
+      // Must use md5 here, to prevent array keys from breaking.
+      if (!isset($path_lookup[md5($path)])) {
+        $source = $_domain;
+        drupal_alter('domain_source', $source, $path, 'path');
+        $path_lookup[md5($path)] = $source;
+      }
+      if ($path_lookup[md5($path)] != $_domain) {
+        $options['absolute'] = TRUE;
+        // In this case, the $base_url cannot have a trailing slash
+        $options['base_url'] = rtrim($source['path'], '/');
+        $target_domain_id = $source['domain_id'];
+        // TODO: merge this code with the above for cleanup.
+      }
+    }
     // We may have to implement hook_domainpath().
     if (!isset($path_rewrite)) {
       $path_rewrite = count(_domain_path_modules());
Index: domain_source/domain_source.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/domain/domain_source/domain_source.module,v
retrieving revision 1.17
diff -u -p -r1.17 domain_source.module
--- domain_source/domain_source.module	24 Oct 2009 21:02:51 -0000	1.17
+++ domain_source/domain_source.module	26 Oct 2009 20:47:55 -0000
@@ -180,7 +180,7 @@ function domain_source_nodeapi(&$node, $
       break;
     case 'load':
       if ($node->nid) {
-        $source = domain_source_lookup($node, $node->domains, $node->domain_site);
+        $source = domain_source_lookup($node->nid, $node->domains, $node->domain_site);
         $node->domain_source = $source['domain_id'];
       }
       break;
@@ -190,7 +190,7 @@ function domain_source_nodeapi(&$node, $
       // calls that are neither a teaser nor a page view.
       if ($a3 !== FALSE || $a4 !== FALSE) {
         if (variable_get('domain_debug', 0) && user_access('set domain access') && isset($node->domain_source)) {
-          $source = domain_lookup($node->domain_source);
+          $source = domain_get_node_match($node->nid);
           $extra = ' ';
           $use_active = db_result(db_query("SELECT domain_id FROM {domain_source} WHERE nid = %d", $node->nid));
           if (empty($use_active)) {
@@ -218,7 +218,9 @@ function domain_source_nodeapi(&$node, $
  */
 function domain_source_lookup($nid, $domains = array(), $domain_site = FALSE) {
   global $_domain;
+
   $source = db_result(db_query("SELECT domain_id FROM {domain_source} WHERE nid = %d", $nid));
+
   if (empty($source)) {
     $source = variable_get('domain_default_source', 0);
   }
@@ -244,6 +246,27 @@ function domain_source_lookup($nid, $dom
 }
 
 /**
+ * Implement hook_domain_source_alter().
+ *
+ * @param &$source
+ *   The domain array from domain_get_node_match(), passed by reference.
+ * @param $id
+ *   The identifier of the obect being rewritten. For nodes, this is the node id.
+ *   In other instances, we may pass a path or other variable.
+ * @param $type
+ *   The type of lookup being performed. (Defaults to 'node'. This value is here
+ *   so that we can eventually rewrite non-node paths.)
+ * @return
+ *   No return value; modify $source by reference.
+ */
+function domain_source_domain_source_alter(&$source, $id, $type = 'node') {
+  // We only act on nodes right now.
+  if ($type == 'node') {
+    $source = domain_source_lookup($id);
+  }
+}
+
+/**
  * FormAPI function that lets us update access rules.
  */
 function domain_source_update_nodes($form, &$form_state) {
