You are here

function _userreference_potential_references_views in Content Construction Kit (CCK) 6.3

Same name and namespace in other branches
  1. 6.2 modules/userreference/userreference.module \_userreference_potential_references_views()

Helper function for _userreference_potential_references(): case of Views-defined referenceable users.

1 call to _userreference_potential_references_views()
_userreference_potential_references in modules/userreference/userreference.module
Fetch an array of all candidate referenced users.

File

modules/userreference/userreference.module, line 700
Defines a field type for referencing a user from a node.

Code

function _userreference_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', 'name');

    // Additional options to let content_plugin_display_references::query()
    // narrow the results.
    $options = array(
      'table' => 'users',
      'field_string' => 'name',
      'string' => $string,
      'match' => $match,
      'field_id' => 'uid',
      '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 name field, so add it if not present (unlikely, but...)
    $fields = $view
      ->get_items('field', $display);
    if (!isset($fields['name'])) {
      $view
        ->add_item($display, 'field', 'users', 'name');
    }

    // 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;
}