A patch to add more token support, including node tokens (for pathauto).

Comments

agentrickard’s picture

Status: Active » Needs review
StatusFileSize
new2.08 KB

And the patch.

dave reid’s picture

Should [node:domains] be plural? I guess if domain.module itself support multiple domains per node, and it's domain_source that assigns a 'source' domain. Maybe this token should be [node:domain-source:*]?

Also, the current code would not output anything if the token [node:domains] would be used.

agentrickard’s picture

StatusFileSize
new2.21 KB

Fixes.

dave reid’s picture

Status: Needs review » Needs work
+++ b/domain.tokens.incundefined
@@ -97,16 +107,27 @@ function domain_tokens($type, $tokens, array $data = array(), array $options = a
+    $domain = domain_get_node_match($data['node']->nid);
+    if ($domain != -1) {
+      $domain_tokens = token_find_with_prefix($tokens, 'domain');
+      $replacements += token_generate('domain', $domain_tokens, array('domain' => $domain), $options);
+      $subdomain = $domain['subdomain'];
+      $replacements['[node:domain]'] = $sanitize ? check_plain($subdomain) : $subdomain;

The above could will generate all tokens no matter if the user actually provided them or not - something that the new Token API in D7 helps avoid now. To generate the tokens on-demand, we should use the following code instead:

  if ($type == 'node' && !empty($data['node'])) {
    $node = $data['node'];

    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'domain':
          $domain = domain_get_node_match($node->nid);
          if ($domain != -1) {
            $replacements[$original] = $sanitize ? check_plain($domain['subdomain']) : $domain['subdomain'];
          }
          break;
      }
    }

    // [node:domain:*] chained token replacement.
    if ($domain_tokens = token_find_with_prefix($tokens, 'domain')) {
      $domain = domain_get_node_match($node->nid);
      if ($domain != -1) {
        $replacements += token_generate('domain', $domain_tokens, array('domain' => $domain), $options);
      }
    }
  }

I checked that the domain_get_node_match() is statically cached so calling it more than once is completely fine.

agentrickard’s picture

Status: Needs work » Needs review
StatusFileSize
new2.83 KB

Like so?

I put the domain killswitch up higher in the stack. Likely that domain_get_node_match() has already been invoked before we get here.

agentrickard’s picture

StatusFileSize
new2.83 KB

Let's not loop-de-loop, shall we?

dave reid’s picture

#6 seems good for now - would just be good to add some tests on it but it shouldn't hold this up.

agentrickard’s picture

Status: Needs review » Fixed

Committing to 7.x.3.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.