You are here

function nodereferrer_referrers in NodeReferrer 7

Same name and namespace in other branches
  1. 5 nodereferrer.module \nodereferrer_referrers()
  2. 6 nodereferrer.module \nodereferrer_referrers()

Gets an array of referrer nids, by node.type & field.type

Parameters

nid: the nid we want to find referres for

fieldnames: array of fieldnames to be checked for referrers

nodetypes: array of node types to be checked for referrers

translations: boolean if true, also return nodes that referrer to translations of the given node

4 calls to nodereferrer_referrers()
nodereferrer_field_load in ./nodereferrer.module
implements hook_field_load().
nodereferrer_field_OLD in ./nodereferrer.module
Implements of hook_field().
nodereferrer_referrers_with_translations in views/nodereferrer.views.inc
Given a node id, field names and content names, returns the list of nodes that refer to the given node. This takes translation settings into account
_nodereferrer_manage_cache in ./nodereferrer.module
Helper function for Node API Hooks.

File

./nodereferrer.module, line 698
Defines a field type for backlinking referencing nodes.

Code

function nodereferrer_referrers($nid, $fieldnames = array(), $nodetypes = array(), $translations = 0, $order = 'DESC') {
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'node', '=')
    ->propertyCondition('status', NODE_PUBLISHED, '=');
  if ($nodetypes) {
    $query
      ->entityCondition('bundle', $nodetypes, 'IN');
  }

  // Order the query.
  switch ($order) {
    case 'TITLE_ASC':
      $query
        ->propertyOrderBy('title', 'ASC');
      break;
    case 'TITLE_DESC':
      $query
        ->propertyOrderBy('title', 'DESC');
      break;
    case 'ASC':
    case 'CREATED_ASC':
      $query
        ->propertyOrderBy('created', 'ASC');
      break;
    case 'DESC':
    case 'CREATED_DESC':
    default:
      $query
        ->propertyOrderBy('created', 'DESC');
      break;
  }
  $fields = _nodereferrer_nodereference_field_options();
  if (empty($fieldnames)) {
    $fieldnames = array_keys($fields);
  }
  $values = array();
  foreach ($fieldnames as $fieldname) {
    $current_query = clone $query;

    // @todo check for translation.
    $current_query
      ->fieldCondition($fieldname, 'nid', $nid, '=');
    $result = $current_query
      ->execute();
    if (!empty($result['node'])) {
      foreach ($result['node'] as $ref_node) {

        /*if (!empty($nodetypes) && !isset($nodetypes[$ref_node->type])) {
          	continue;
          }*/

        // avoid duplicate referrers by using nid as key
        $values[$ref_node->nid]['nid'] = $ref_node->nid;
        $values[$ref_node->nid]['vid'] = $ref_node->vid;

        //$values[$ref_node->nid]['title'] = $ref_node->title;

        //Store the nodereference fieldnames in the nodereferrer data in the node,

        //so that it can be looked up without needing to load the referring node.
        $values[$ref_node->nid]['referrers'][$fieldname] = $fieldname;
      }
    }
  }
  return $values;

  /*
    if ($nodetypes) {
      $filter_nodetypes = "AND n.type IN ('" . implode("', '", $nodetypes) . "')";
    }
    else {
      $filter_nodetypes = '';
    }
    $fields = content_fields();
    // Set default values of fieldnames.
    if (!count($fieldnames)) {
      $fieldnames = array_keys($fields);
    }
    $values = array();
    foreach ($fieldnames as $fieldname) {
      if ($fields[$fieldname]['type'] == 'nodereference') {
        $db_info = content_database_info($fields[$fieldname]);

        if ($translations) {
          $query = "SELECT       n.nid, n.vid, n.title $select_field
                    FROM         {" . $db_info['table'] . "} nr
                    INNER JOIN   {node} current_node ON current_node.nid = %d
                    INNER JOIN   {node} n ON n.vid = nr.vid AND n.status = 1 " . $filter_nodetypes . "
                    LEFT JOIN    {node} translations ON current_node.tnid > 0 AND translations.tnid = current_node.tnid
                    WHERE        (current_node.tnid = 0 AND nr." . $db_info['columns']['nid']['column'] . " = current_node.nid)
                                 OR
                                 (current_node.tnid > 0 AND nr." . $db_info['columns']['nid']['column'] . " = translations.nid)
                    ORDER BY     " . $order;
        }
        else {
          $query = "SELECT       n.nid, n.vid, n.title $select_field
                    FROM         {" . $db_info['table'] . "} nr
                    INNER JOIN   {node} n ON n.vid = nr.vid AND n.status = 1 " . $filter_nodetypes . "
                    WHERE        nr." . $db_info['columns']['nid']['column'] . " = %d
                    ORDER BY     " . $order;
        }

        $query = db_rewrite_sql($query);
        $result = db_query($query, $nid);

        while ($value = db_fetch_array($result)) {
          // avoid duplicate referrers by using nid as key
          $values[$value['nid']]['nid'] = $value['nid'];
          $values[$value['nid']]['vid'] = $value['vid'];
          $values[$value['nid']]['title'] = $value['title'];
          //Store the nodereference fieldnames in the nodereferrer data in the node,
          //so that it can be looked up without needing to load the referring node.
          $values[$value['nid']]['referrers'][$fieldname] = $fieldname;
        }
      }
    }
    return $values;
  */
}