You are here

function select2widget_entityreference_get_matches in Select2 Field Widget 7.2

Return JSON based on given field, instance and string.

This function can be used by other modules that wish to pass a mocked definition of the field on instance.

Parameters

$field: The field array defintion.

$instance: The instance array defintion.

$entity_type: The entity type.

$entity_id: Optional; The entity ID the entity-reference field is attached to. Defaults to ''.

$string: The label of the entity to query by.

File

./select2widget.entityreference.inc, line 172

Code

function select2widget_entityreference_get_matches($field, $instance, $entity_type, $string, $entity_id = '') {
  $matches = array();
  $options = array();
  $entity = NULL;
  if ($entity_id !== 'NULL') {
    $entity = entity_load_single($entity_type, $entity_id);
    if (!$entity || !entity_access('view', $entity_type, $entity)) {
      return MENU_ACCESS_DENIED;
    }
  }
  if ($instance['widget']['type'] == 'og_complex') {
    $instance['field_mode'] = 'default';

    // Set the current field mode if passed in.
    if (!empty($_GET['field_mode']) && in_array($_GET['field_mode'], array(
      'admin',
      'default',
    ))) {
      $instance['field_mode'] = $_GET['field_mode'];
    }
    $settings = isset($instance['settings']['behaviors']['og_widget'][$instance['field_mode']]['widget_settings']['select2widgetajax']) ? $instance['settings']['behaviors']['og_widget'][$instance['field_mode']]['widget_settings']['select2widgetajax'] : array();
  }
  else {
    $settings = $instance['widget']['settings']['select2widgetajax'];
  }
  $handler = entityreference_get_selection_handler($field, $instance, $entity_type, $entity);
  $entity_labels = $handler
    ->getReferencableEntities($string, $settings['match_operator'], abs($settings['match_limit']));
  $matches = select2widget_render_modes($entity_labels, $settings['view_mode'], $field['settings']['target_type']);
  drupal_json_output($matches);
}