Commit 2ec306b7 authored by hass's avatar hass
Browse files

Issue #1722112 by hass: Autocomplete for entityreference fields

parent 32413b13
...@@ -38,19 +38,28 @@ function realname_menu() { ...@@ -38,19 +38,28 @@ function realname_menu() {
$items['realname/autocomplete'] = array( $items['realname/autocomplete'] = array(
'page callback' => 'realname_autocomplete', 'page callback' => 'realname_autocomplete',
'page arguments' => array('', '', 2),
'access arguments' => array('access user profiles'), 'access arguments' => array('access user profiles'),
'type' => MENU_CALLBACK, 'type' => MENU_CALLBACK,
); );
$items['realname/autocomplete/%/%/%'] = array(
'page callback' => 'realname_autocomplete',
'page arguments' => array(2, 3, 4),
'access callback' => 'realname_autocomplete_access_callback',
'access arguments' => array(2, 3, 4),
'type' => MENU_CALLBACK,
);
return $items; return $items;
} }
/** /**
* Implements hook_menu_alter(). * Implements hook_menu_alter() to replace user autocomplete with realname autocomplete.
*/ */
function realname_menu_alter(&$items) { function realname_menu_alter(&$items) {
// Implement realname autocomplete. // Implement realname autocomplete.
$items['user/autocomplete']['page callback'] = 'realname_autocomplete'; $items['user/autocomplete']['page callback'] = 'realname_autocomplete';
$items['user/autocomplete']['page arguments'] = array('', '', 2);
} }
/** /**
...@@ -314,28 +323,92 @@ function realname_delete_all() { ...@@ -314,28 +323,92 @@ function realname_delete_all() {
* Menu callback; Retrieve a JSON object containing autocomplete suggestions * Menu callback; Retrieve a JSON object containing autocomplete suggestions
* for existing users based on their generated real names. * for existing users based on their generated real names.
*/ */
function realname_autocomplete($string = '') { function realname_autocomplete($field_name, $entity_type, $bundle_name, $string = '') {
$instance = field_info_instance($entity_type, $field_name, $bundle_name);
$matches = array(); $matches = array();
if ($string) { // User entityreference autocomplete fields.
if ($instance['widget']['type'] == 'entityreference_autocomplete' && !empty($string)) {
$query = db_select('users', 'u');
$query->leftJoin('realname', 'rn', 'u.uid = rn.uid');
$query->fields('u', array('uid', 'name'));
if ($instance['widget']['settings']['match_operator'] == 'CONTAINS') {
$query->condition(db_or()
->condition('rn.realname', '%' . db_like($string) . '%', 'LIKE')
->condition('u.name', '%' . db_like($string) . '%', 'LIKE')
);
}
else {
$query->condition(db_or()
->condition('rn.realname', db_like($string) . '%', 'LIKE')
->condition('u.name', db_like($string) . '%', 'LIKE')
);
}
$query->range(0, 10);
$uids = $query->execute()->fetchCol();
$accounts = user_load_multiple($uids);
foreach ($accounts as $account) {
if ($matches[$account->name] != $account->name) {
$matches[t('!account (@uid)', array('!account' => format_username($account), '@uid' => $account->uid))] = t('!realname (@username)', array('!realname' => format_username($account), '@username' => $account->name));
}
}
}
// User autocomplete fields.
elseif (!empty($string)) {
$query = db_select('users', 'u'); $query = db_select('users', 'u');
$query->leftJoin('realname', 'rn', 'u.uid = rn.uid'); $query->leftJoin('realname', 'rn', 'u.uid = rn.uid');
$query->fields('u', array('uid')); $query->fields('u', array('uid', 'name'));
$name_like = db_or(); $query->condition(db_or()
$name_like->condition('rn.realname', db_like($string) . '%', 'LIKE'); ->condition('rn.realname', db_like($string) . '%', 'LIKE')
$name_like->condition('u.name', db_like($string) . '%', 'LIKE'); ->condition('u.name', db_like($string) . '%', 'LIKE')
$query->condition($name_like); );
$query->range(0, 10); $query->range(0, 10);
$uids = $query->execute()->fetchCol(); $uids = $query->execute()->fetchCol();
$accounts = user_load_multiple($uids); $accounts = user_load_multiple($uids);
foreach ($accounts as $account) { foreach ($accounts as $account) {
$matches[$account->name] = format_username($account);
if ($matches[$account->name] != $account->name) { if ($matches[$account->name] != $account->name) {
$matches[$account->name] .= ' (' . check_plain($account->name) . ')'; $matches[$account->name] = t('!realname (@username)', array('!realname' => format_username($account), '@username' => $account->name));
} }
} }
} }
drupal_json_output($matches); drupal_json_output($matches);
} }
/**
* Implements hook_widget_form_alter().
*
* Overrides entityreference autocomplete fields that point to an entity
* of type 'user'.
*/
function realname_field_widget_form_alter(&$element, &$form_state, $context) {
if ($context['field']['type'] == 'entityreference') {
if ($context['field']['settings']['target_type'] == 'user') {
$element['target_id']['#autocomplete_path'] = 'realname/autocomplete/' . $context['field']['field_name'] . '/' . $context['instance']['entity_type'] . '/' . $context['instance']['bundle'];
}
}
}
/**
* Menu Access callback for the autocomplete widget.
*
* @param $field_name
* The name of the entity-reference field.
* @param $entity_type
* The entity type.
* @param $bundle_name
* The bundle name.
* @return
* True if user can access this menu item.
*/
function realname_autocomplete_access_callback($field_name, $entity_type, $bundle_name) {
$field = field_info_field($field_name);
$instance = field_info_instance($entity_type, $field_name, $bundle_name);
if (!$field || !$instance || $field['type'] != 'entityreference' || !field_access('edit', $field, $entity_type)) {
return FALSE;
}
return TRUE;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment