You are here

function _rolereference_potential_references_views in Role Reference 6

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

1 call to _rolereference_potential_references_views()
_rolereference_potential_references in ./rolereference.module
Fetch an array of all candidate referenced roles.

File

./rolereference.module, line 709
Defines a field type for referencing a role. Based almost entirely on nodereference and userreference modules.

Code

function _rolereference_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' => 'role',
      'field_string' => 'name',
      'string' => $string,
      'match' => $match,
      'field_id' => 'rid',
      '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.
    if (isset($limit)) {
      $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', 'role', '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;
}