You are here

function domain_tokens in Domain Access 7.3

Same name and namespace in other branches
  1. 8 domain/domain.module \domain_tokens()
  2. 7.2 domain.tokens.inc \domain_tokens()

Implements hook_tokens().

File

./domain.tokens.inc, line 71
Builds placeholder replacement tokens for domain-related data.

Code

function domain_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $sanitize = !empty($options['sanitize']);
  $replacements = array();

  // Base token handling.
  if ($type == 'domain' && !empty($data['domain'])) {
    $domain = $data['domain'];

    // Loop through the tokens and replace the elements.
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'id':
          $replacements[$original] = $domain['domain_id'];
          break;
        case 'machine-name':
        case 'machine_name':

          // Deprecated and renamed to 'machine-name'.
          $replacements[$original] = $domain['machine_name'];
          break;
        case 'name':
          $replacements[$original] = $sanitize ? check_plain($domain['sitename']) : $domain['sitename'];
          break;
        case 'path':
          if (!isset($domain['path'])) {
            $domain['path'] = domain_get_path($domain);
          }
          $replacements[$original] = $domain['path'];
          break;
        case 'url':
          $url = domain_url_encode($domain['subdomain']);
          $replacements[$original] = $sanitize ? check_plain($url) : $url;
          break;
        case 'subdomain':
          $subdomain_elements = explode('.', $domain['subdomain']);
          if (count($subdomain_elements) > 2) {
            $subdomain = $subdomain_elements[0];
          }
          else {
            $subdomain = 'www';
          }
          $subdomain = domain_url_encode($subdomain);
          $replacements[$original] = $sanitize ? check_plain($subdomain) : $subdomain;
          break;
        case 'hostname':
          $subdomain = $domain['subdomain'];
          $replacements[$original] = $sanitize ? check_plain($subdomain) : $subdomain;
          break;
      }
    }
  }

  // Node tokens.
  if ($type == 'node' && !empty($data['node']->nid)) {
    $node = $data['node'];
    $domain = domain_get_node_match($node->nid);
    if (empty($domain) || $domain == -1) {
      return $replacements;
    }

    // Loop through the tokens to not waste cycles.
    foreach ($tokens as $name => $original) {
      if ($name == 'domain') {
        $replacements[$original] = $sanitize ? check_plain($domain['sitename']) : $domain['sitename'];
      }
    }
    if ($domain_tokens = token_find_with_prefix($tokens, 'domain')) {
      $replacements += token_generate('domain', $domain_tokens, array(
        'domain' => $domain,
      ), $options);
    }
  }
  elseif ($type == 'current-domain') {
    $current_domain = domain_get_domain();
    $replacements += token_generate('domain', $tokens, array(
      'domain' => $current_domain,
    ), $options);
  }
  elseif ($type == 'default-domain') {
    $default_domain = domain_default(FALSE);
    $replacements += token_generate('domain', $tokens, array(
      'domain' => $default_domain,
    ), $options);
  }
  return $replacements;
}