You are here

function _nodereference_potential_references in Content Construction Kit (CCK) 6

Same name and namespace in other branches
  1. 5 nodereference.module \_nodereference_potential_references()
  2. 6.3 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.

4 calls to _nodereference_potential_references()
nodereference_allowed_values in modules/nodereference/nodereference.module
Implementation of hook_allowed_values().
nodereference_autocomplete in modules/nodereference/nodereference.module
Retrieve a pipe delimited string of autocomplete suggestions
nodereference_autocomplete_validate in modules/nodereference/nodereference.module
Validate an autocomplete element.
nodereference_field in modules/nodereference/nodereference.module
Implementation of hook_field().

File

modules/nodereference/nodereference.module, line 535
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
    $view
      ->init();

    // TODO is this the right way to do this?
    // make sure the fields get included in the query.
    // TODO Which display should we use here?
    // In our Views 1 to Views 2 updates we are
    // putting fields in the 'default' display.
    $display = 'default';
    $view
      ->set_display($display);
    $view->display[$display]->style_plugin = 'list';

    // Add arguments for the view.
    if (isset($field['advanced_view_args'])) {

      // TODO: Support Tokens using token.module ?
      $view_args = array();
      $view_args = array_map('trim', explode(',', $field['advanced_view_args']));
      $view
        ->set_arguments($view_args);
    }
    if (isset($string)) {

      //We do need title field, so add it if not present (unlikely, but...)
      if (!array_key_exists('title', $view->display[$display]->display_options['fields'])) {
        $view
          ->add_item($display, 'field', 'node', 'title');
      }

      // We use the title filter in the view, so make sure it's there.
      if (!array_key_exists('title', $view->display[$display]->display_options['filters'])) {
        $view
          ->add_item($display, 'filter', 'node', 'title');
      }

      // TODO Is this the best way to set values?
      $view->display[$display]->display_options['filters']['title']['value'] = $string;
      $view->display[$display]->display_options['filters']['title']['operator'] = $exact_string ? '=' : 'contains';
    }

    // Make sure the query is not cached
    $view->is_cacheable = FALSE;
    $view
      ->render();
    $options = array();
    foreach ($view->result as $row) {
      foreach ($view->field as $field) {
        if (!empty($field['handler']) && is_object($field['handler'])) {
          $options[$row->nid][] = theme('views_view_field', $view, $field, $row);
        }
      }
    }
    return $options;
  }
  else {

    // standard field : referenceable nodes defined by content types
    // build the appropriate query
    $related_types = array();
    $args = array();
    if (is_array($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 (isset($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);
  }
  $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;
}