You are here

function _nodereference_potential_references in Content Construction Kit (CCK) 5

Same name and namespace in other branches
  1. 6.3 modules/nodereference/nodereference.module \_nodereference_potential_references()
  2. 6 modules/nodereference/nodereference.module \_nodereference_potential_references()
  3. 6.2 modules/nodereference/nodereference.module \_nodereference_potential_references()

Fetch an array of all candidate referenced nodes, for use in presenting the selection form to the user.

5 calls to _nodereference_potential_references()
hook_widget in ./field.php
Define the behavior of a widget.
nodereference_autocomplete in ./nodereference.module
Retrieve a pipe delimited string of autocomplete suggestions
nodereference_field in ./nodereference.module
Implementation of hook_field().
nodereference_widget in ./nodereference.module
Implementation of hook_widget().
_nodereference_filter_handler in ./nodereference.module
Provide a list of nodes to filter on.

File

./nodereference.module, line 405
Defines a field type for referencing one node from another.

Code

function _nodereference_potential_references($field, $return_full_nodes = FALSE, $string = '', $exact_string = false) {
  if (module_exists('views') && isset($field['advanced_view']) && $field['advanced_view'] != '--' && ($view = views_get_view($field['advanced_view']))) {

    // advanced field : referenceable nodes defined by a view
    // let views.module build the query
    // arguments for the view
    $view_args = array();
    if (isset($field['advanced_view_args'])) {

      // TODO: Support Tokens using token.module ?
      $view_args = array_map(trim, explode(',', $field['advanced_view_args']));
    }
    if (isset($string)) {
      views_view_add_filter($view, 'node', 'title', $exact_string ? '=' : 'contains', $string, null);
    }

    // we do need title field, so add it if not present (unlikely, but...)
    $has_title = array_reduce($view->field, create_function('$a, $b', 'return ($b["field"] == "title") || $a;'), false);
    if (!$has_title) {
      views_view_add_field($view, 'node', 'title', '');
    }
    views_load_cache();
    views_sanitize_view($view);

    // make sure the fields get included in the query
    $view->page = true;
    $view->page_type = 'list';

    // make sure the query is not cached
    unset($view->query);

    // Views 1.5-
    $view->is_cacheable = FALSE;

    // Views 1.6+
    $view_result = views_build_view('result', $view, $view_args);
    $result = $view_result['result'];
  }
  else {

    // standard field : referenceable nodes defined by content types
    // build the appropriate query
    $related_types = array();
    $args = array();
    if (isset($field['referenceable_types'])) {
      foreach ($field['referenceable_types'] as $related_type) {
        if ($related_type) {
          $related_types[] = " n.type = '%s'";
          $args[] = $related_type;
        }
      }
    }
    $related_clause = implode(' OR ', $related_types);
    if (!count($related_types)) {
      return array();
    }
    if ($string !== '') {
      $string_clause = $exact_string ? " AND n.title = '%s'" : " AND n.title LIKE '%%%s%'";
      $related_clause = "(" . $related_clause . ")" . $string_clause;
      $args[] = $string;
    }
    $result = db_query(db_rewrite_sql("SELECT n.nid, n.title AS node_title, n.type AS node_type FROM {node} n WHERE " . $related_clause . " ORDER BY n.title, n.type"), $args);
  }
  if (db_num_rows($result) == 0) {
    return array();
  }
  $rows = array();
  while ($node = db_fetch_object($result)) {
    if ($return_full_nodes) {
      $rows[$node->nid] = $node;
    }
    else {
      $rows[$node->nid] = $node->node_title;
    }
  }
  return $rows;
}