Override entityreference autocomplete and return entityfieldquery output I am trying to override an entityreference autocomplete form item, I have managed to override the form, and get the arg passed to a hook_menu callback. However, I'm struggling with getting the callback to work based on what I type into the form box. Looking in the Entity reference module there is some code in the hook_autocomplete_callback that handles the $string argument and looks up matches $entity_labels = $handler-&gt;getReferencableEntities($tag_last, $instance['widget']['settings']['match_operator'] - something like that. 

Can anyone assist? 

My code:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function wl_event_form_event_node_form_alter(&amp;$form, &amp;$form_state, $form_id) {
  dpm($form);
  // We will get our term id argument from the from build itself.
    $node_id  = $form['#node']-&gt;nid;
  // This is the path we will create in hook_menu().
     $new_path = "wl_event/autocomplete/{$node_id}";
  // Maximum number of descrete values (deltas) that are present.
 $max_delta = $form['field_wl_event_acquired_resource']['und']['#max_delta'];
  // Hijack the autocomplete callback for each of our values.
  for($x=0; $x &lt;= $max_delta; $x++) {
    $form['field_wl_event_acquired_resource']['und'][$x]['target_id']['#autocomplete_path']= $new_path;
  }
}

/**
 * Implements hook_menu().
 */
// can be used to do a lookup on a menu path to return json
// probably entity reference autocomplete does a similar thing

//we want to get all of the resources of the user profiles of
//users who are registered on the event

//
function wl_event_menu() {
  $items = array();
  $items['wl_event/autocomplete/%'] = array(
    'title' =&gt; t('project call back'),
    'page callback' =&gt; 'wl_event_autocomplete_callback',
    'page arguments' =&gt; array(2),
    'access callback' =&gt; TRUE,
    'type' =&gt; MENU_CALLBACK
  );
  return $items;
}

function wl_event_autocomplete_callback($arg1, $string = '') {
  $query = new EntityFieldQuery();
  $query-&gt;entityCondition('entity_type', 'node')
  -&gt;entityCondition('bundle', 'resource');
  // -&gt;propertyCondition('nid', '1')
  $results = $query-&gt;execute();
  print_r(drupal_json_output($results));
  return drupal_json_output($results);
}
EntityReference uses the ctools plugin system to define selection handlers which feed into the autocomplete options.  The interface definition is defined in entityreference/plugins/selection/abstract.inc.  In that same directory, you will see the two included handlers, Simple and Views.  Each of these is defined by two files, one for the class itself, and one with an array to register the plugin with ctools.

To provide your own selection handler, first tell ctools where to look for your plugin by implementing hook_ctools_plugin_directory.

/**
 * Implements hook_ctools_plugin_directory().
 */
function wl_event_ctools_plugin_directory($module, $plugin) {
  if ($module == 'entityreference') {
    return 'plugins/' . $plugin;
  }
}


Then create 2 files in wl_event/plugins/selection similar to those in the entityreference.  The .inc file should define the ctools plugin info, and the .class.php file should contain your plugin class.  Most likely you'll want to subclass EntityReference_SelectionHandler_Generic and just override the appropriate methods.

Lastly, make sure you add the file with your plugin class to the files[] array in your modules info file so that the autoloader can find it.