You are here

function domain_source_lookup in Domain Access 7.2

Same name and namespace in other branches
  1. 6.2 domain_source/domain_source.module \domain_source_lookup()
  2. 7.3 domain_source/domain_source.module \domain_source_lookup()

Get the source domains for multiple matches, mimicking node_load().

Parameters

$nodes: An array of nodes, keyed by node id, or a single node id.

$domains: An optional array of domain ids; can only be used for a single node.

$domain_site: Optional flag indicating that the node is assigned to all domains.

$reset: Force a reset of the static data lookup.

Return value

An associative array of data, keyed by node id, or a single array. The data array is the active source domain to use, as returned by domain_lookup().

5 calls to domain_source_lookup()
domain_source_domain_source_alter in domain_source/domain_source.module
Implements hook_domain_source_alter().
domain_source_node_insert in domain_source/domain_source.module
Implements hook_node_insert()
domain_source_node_load in domain_source/domain_source.module
Implements hook_node_load().
domain_source_validate in domain_source/domain_source.module
Form validation step
hook_domain_source_alter in ./domain.api.php
Allows modules to specify the target link for a node.

File

domain_source/domain_source.module, line 269
Creates a source domain for linking to content from other domains.

Code

function domain_source_lookup($nodes, $domains = array(), $domain_site = FALSE, $reset = FALSE) {
  global $_domain;
  static $lookups;

  // Reset?
  if ($reset) {
    $lookups = array();
  }

  // Ensure we format the lookup correctly.
  if (!is_array($nodes)) {
    $nid = $nodes;
    $node_ids = array(
      $nid,
    );
    $array = FALSE;
  }
  else {
    $node_ids = array_keys($nodes);
    $array = TRUE;
  }

  // Static cache.
  if (!$reset && isset($nid) && isset($lookups[$nid])) {
    return $lookups[$nid];
  }
  $result = db_query("SELECT nid, domain_id FROM {domain_source} WHERE nid IN (:nid)", array(
    ':nid' => $node_ids,
  ))
    ->fetchAll();
  foreach ($result as $data) {
    $source = $data->domain_id;

    // DOMAIN_SOURCE_USE_ACTIVE is the status for 'Use active domain.'
    if ($source == DOMAIN_SOURCE_USE_ACTIVE) {
      $source = $_domain['domain_id'];
    }

    // When called from url_rewrite_outbound(), we might have no data,
    // but if $nodes is populated, then we have run domain_node_load().
    if (is_array($nodes) && isset($nodes[$data->nid]->domains)) {
      $domains = $nodes[$data->nid]->domains;
      $domain_site = $nodes[$data->nid]->domain_site;
    }
    if (empty($domains)) {
      $lookup = domain_get_node_domains($data->nid);
      $domain_site = $lookup['domain_site'];
      $domains = $lookup['domain_id'];
    }

    // domain_get_node_domains() returns the default (0) domain as -1, so we
    // must transform it here before running a check.
    $source = $source == 0 ? -1 : $source;

    // If no valid source is found, take the first match from the $domains array.
    if (empty($domain_site) && !empty($domains) && !in_array($source, $domains)) {
      $source = current($domains);
    }

    // Convert the source id from -1 to 0 and run the lookup.
    $domain_id = $source == -1 ? 0 : $source;

    // Lookup the source domain.
    $lookups[$data->nid] = domain_lookup($domain_id);
  }

  // Make sure we found something for every requested node.
  $no_records = $node_ids;
  if (!empty($lookups)) {
    $no_records = array_diff($node_ids, array_keys($lookups));
  }
  foreach ($no_records as $nid) {
    $default_source = variable_get('domain_default_source', 0);

    // Global setting for 'do not change domain.'
    if ($default_source == -1) {
      $lookups[$nid] = $_domain;
    }
    else {
      $lookups[$nid] = domain_lookup($default_source);
    }
  }
  if ($array) {
    return $lookups;
  }
  return $lookups[$nid];
}