You are here

function ext_search_node_ref_potential_references in Extended search page 7

Fetch an array of all candidate referenced nodes.

Parameters

$page: The page object.

$instance: The field instance.

$string: Optional string to filter titles on (used by autocomplete).

$limit: If non-zero, limit the size of the result set.

Return value

An array of valid nodes in the form: array( nid => array( 'title' => The node title, 'rendered' => The text to display in widgets (can be HTML) ), ... ) @todo Check whether we still need the 'rendered' value (hook_options_list() does not need it anymore). Should probably be clearer after the 'Views' mode is ported.

1 call to ext_search_node_ref_potential_references()
ext_search_node_ref_autocomplete in ext_search_node_ref/ext_search_node_ref.module
Menu callback for the autocomplete results.

File

ext_search_node_ref/ext_search_node_ref.module, line 218
Extended search node reference.

Code

function ext_search_node_ref_potential_references($page, $instance, $keys = '', $limit = 50) {
  $results =& drupal_static(__FUNCTION__, array());
  $field = field_info_field($instance['field_name']);
  $bundles = array();
  foreach ($field['settings']['referenceable_types'] as $bundle => $bundle_ok) {
    if ($bundle_ok) {
      $bundles[] = $bundle;
    }
  }

  // Create unique id for static cache.
  $cid = $instance['field_name'] . ':' . ($keys !== '' ? $keys . ':' : '') . ($limit ? ':' . $limit : '');
  if (!isset($results[$cid])) {
    $references = array();
    if (count($bundles)) {

      // executing search with default filters values
      foreach (ext_search_page_get_filter_widgets($page) as $field => $widget) {
        if (isset($widget['default'])) {
          $values[$field] = $widget['default'];
        }
      }
      $query = ext_search_page_search_query($page, $keys, $values, FALSE, $limit * 3, 0, 'ext_search_node_ref:' . $cid);

      // use indexed type field
      if ($instance['widget']['settings']['use_node_type_index']) {
        $filter = $query
          ->createFilter('OR');
        foreach ($bundles as $bundle) {
          $filter
            ->condition('type', $bundle);
        }
        $query
          ->filter($filter);
      }
      $results = $query
        ->execute();
      if (isset($results['results'])) {
        $i = 0;
        foreach ($results['results'] as $result) {
          $node = entity_load('node', array(
            $result['id'],
          ));
          if (!$node || !count($node)) {
            continue;
          }
          $node = reset($node);
          if (!in_array($node->type, $bundles)) {
            continue;
          }
          if (!node_access('view', $node)) {
            continue;
          }
          $references[$node->nid] = array(
            'title' => $node->title,
            'rendered' => check_plain($node->title),
          );
          $i++;
          if ($i == $limit) {
            break;
          }
        }
      }
    }

    // Store the results.
    $results[$cid] = !empty($references) ? $references : array();
  }
  return $results[$cid];
}