You are here

function _nodereference_potential_references_views in Content Construction Kit (CCK) 6.2

Same name and namespace in other branches
  1. 6.3 modules/nodereference/nodereference.module \_nodereference_potential_references_views()

Helper function for _nodereference_potential_references(): case of Views-defined referenceable nodes.

1 call to _nodereference_potential_references_views()
_nodereference_potential_references in modules/nodereference/nodereference.module
Fetch an array of all candidate referenced nodes.

File

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

Code

function _nodereference_potential_references_views($field, $string = '', $match = 'contains', $ids = array(), $limit = NULL) {
  $view_name = $field['advanced_view'];
  if ($view = views_get_view($view_name)) {

    // We add a display, and let it derive from the 'default' display.
    // TODO: We should let the user pick a display in the fields settings - sort of requires AHAH...
    $display = $view
      ->add_display('content_references');
    $view
      ->set_display($display);

    // TODO from merlinofchaos on IRC : arguments using summary view can defeat the style setting.
    // We might also need to check if there's an argument, and set *its* style_plugin as well.
    $view->display_handler
      ->set_option('style_plugin', 'content_php_array_autocomplete');
    $view->display_handler
      ->set_option('row_plugin', 'fields');

    // Used in content_plugin_style_php_array::render(), to get
    // the 'field' to be used as title.
    $view->display_handler
      ->set_option('content_title_field', 'title');

    // Additional options to let content_plugin_display_references::query()
    // narrow the results.
    $options = array(
      'table' => 'node',
      'field_string' => 'title',
      'string' => $string,
      'match' => $match,
      'field_id' => 'nid',
      'ids' => $ids,
    );
    $view->display_handler
      ->set_option('content_options', $options);

    // TODO : for consistency, a fair amount of what's below
    // should be moved to content_plugin_display_references
    // Limit result set size.
    // - Views 3.x uses set_items_per_page(),
    // - Views 2.x uses set_option('items_per_page').
    $limit = isset($limit) ? $limit : 0;
    if (method_exists($view, 'set_items_per_page')) {
      $view
        ->set_items_per_page($limit);
    }
    else {
      $view->display_handler
        ->set_option('items_per_page', $limit);
    }

    // Get arguments for the view.
    if (!empty($field['advanced_view_args'])) {

      // TODO: Support Tokens using token.module ?
      $view_args = array_map('trim', explode(',', $field['advanced_view_args']));
    }
    else {
      $view_args = array();
    }

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

    // If not set, make all fields inline and define a separator.
    $options = $view->display_handler
      ->get_option('row_options');
    if (empty($options['inline'])) {
      $options['inline'] = drupal_map_assoc(array_keys($view
        ->get_items('field', $display)));
    }
    if (empty($options['separator'])) {
      $options['separator'] = '-';
    }
    $view->display_handler
      ->set_option('row_options', $options);

    // Make sure the query is not cached
    $view->is_cacheable = FALSE;

    // Get the results.
    $result = $view
      ->execute_display($display, $view_args);
  }
  else {
    $result = FALSE;
  }
  return $result;
}