You are here

function references_potential_references_view in References 7.2

Retrieves an array of candidate referenceable entities, defined by a view.

@codingStandardsIgnoreStart

Parameters

string $entity_type: The entity type.

string $view_name: The name of the view.

string $display_name: The name of the view's display. This has to be a 'References' display.

array $args: The array of arguments ("contextual filters") for the view.

array $options: Array of options to limit the scope of the returned list. This parameter is similar to the $options parameter for node_reference_potential_references(). An additional key is required:

  • title_field: the name of the column holding entities 'titles' within the entity base table.

Return value

array An array of entities, in the format expected by node_reference_potential_references().

See also

node_reference_potential_references()

_node_reference_potential_references_views()

2 calls to references_potential_references_view()
_node_reference_potential_references_views in node_reference/node_reference.module
Helper function for node_reference_potential_references().
_user_reference_potential_references_views in user_reference/user_reference.module
Helper function for user_reference_potential_references().

File

./references.module, line 154
Defines common base features for the various reference field types.

Code

function references_potential_references_view($entity_type, $view_name, $display_name, $args, $options) {

  // @codingStandardsIgnoreEnd
  $entity_info = entity_get_info($entity_type);

  // Check that the view is valid and the display still exists.
  $view = views_get_view($view_name);
  if (!$view || $view->base_table != $entity_info['base table'] || !isset($view->display[$display_name])) {
    return FALSE;
  }

  // If we have no access to the View an empty result should be returned to
  // avoid triggering the fallback results.
  if (!$view
    ->access(array(
    $display_name,
  ))) {
    return array();
  }

  // Temporary backwards compatibility for fields migrated from CCK D6: accept
  // 'default' display, but dynamically add a 'references' display out of it.
  if ($display_name == 'default') {
    $display_name = $view
      ->add_display('references');
  }
  $view
    ->set_display($display_name);

  // @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.
  // Set additional options to let references_plugin_display::query() narrow
  // the results.
  $references_options = array(
    'ids' => $options['ids'],
    'title_field' => $options['title_field'],
    'string' => $options['string'],
    'match' => $options['match'],
  );
  $view->display_handler
    ->set_option('references_options', $references_options);

  // We need the title field for autocomplete widgets, so add it (hidden) if not
  // present.
  $fields = $view
    ->get_items('field', $display_name);
  if (!isset($fields[$options['title_field']])) {
    $label_options = array(
      'exclude' => 1,
    );
    $view
      ->add_item($display_name, 'field', $entity_info['base table'], $options['title_field'], $label_options);
  }

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

  // Get the results.
  $results = $view
    ->execute_display($display_name, $args);
  return $results;
}