You are here

function domain_get_node_domains in Domain Access 7.3

Same name and namespace in other branches
  1. 6.2 domain.module \domain_get_node_domains()
  2. 7.2 domain.module \domain_get_node_domains()

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

Parameters

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

Return value

An array of data, keyed by node id, or a single array for a single node. The data array, consists of two parts. 'domain_id' is an array of active domain ids. 'domain_site' is a TRUE/FALSE boolean indicating affiliate status.

2 calls to domain_get_node_domains()
domain_content_update_nodes in domain_content/domain_content.module
Abstraction function that lets us update access rules.
domain_node_load in ./domain.module
Implements hook_node_load().
1 string reference to 'domain_get_node_domains'
_domain_store_grants in ./domain.module
Store node_access records in the {domain_access} table.

File

./domain.module, line 1976
Core module functions for the Domain Access suite.

Code

function domain_get_node_domains($nodes) {
  $lookup =& drupal_static(__FUNCTION__, array());

  // Ensure we form our data properly.
  if (!is_array($nodes)) {
    $node_ids[$nodes] = $nodes;
    $array = FALSE;
  }
  else {
    $node_ids = $nodes;
    $array = TRUE;
  }

  // If not an array, just return the requested data.
  if (!$array && isset($lookup[$nodes])) {
    return $lookup[$nodes];
  }

  // Set the proper value for the node, but include a default.
  $domains = array(
    'domain_id' => array(),
    'domain_site' => FALSE,
  );
  $records = array();
  $result = db_query("SELECT nid, gid, realm FROM {domain_access} WHERE nid IN (:nid)", array(
    ':nid' => array_keys($node_ids),
  ));

  // While this should return records for every node, we cannot guarantee success.
  foreach ($result as $data) {
    if ($data->realm == 'domain_id') {
      $records[$data->nid]['domain_id'][$data->gid] = $data->gid;
    }
    elseif ($data->realm == 'domain_site') {
      $records[$data->nid]['domain_site'] = TRUE;
    }
  }

  // Run through the nodes and ensure they have proper data.
  foreach ($node_ids as $nid => $value) {
    $lookup[$nid] = $domains;
    foreach (array(
      'domain_id',
      'domain_site',
    ) as $key) {
      if (isset($records[$nid][$key])) {
        $lookup[$nid][$key] = $records[$nid][$key];
      }
    }
  }

  // Return all data or just a single node?
  if ($array) {
    return $lookup;
  }
  if (isset($lookup[$nodes])) {
    return $lookup[$nodes];
  }
  return $domains;
}