Skip to content

Instantly share code, notes, and snippets.

@paulsheldrake
Last active June 27, 2018 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulsheldrake/9f1bb3ad8c1ab8f8525f32fbf5247509 to your computer and use it in GitHub Desktop.
Save paulsheldrake/9f1bb3ad8c1ab8f8525f32fbf5247509 to your computer and use it in GitHub Desktop.
diff --git a/acl.admin.inc b/acl.admin.inc
index 41b3822..4f0e5ed 100644
--- a/acl.admin.inc
+++ b/acl.admin.inc
@@ -8,6 +8,7 @@
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Database\Database;
+use Drupal\Core\Entity\Element\EntityAutocomplete;
/**
* Implementation of acl_edit_form().
@@ -69,11 +70,12 @@ function _acl_edit_form($acl_id, $label = NULL, $new_acl = FALSE) {
);
$form['add'] = array(
- '#type' => 'textfield',
+ '#type' => 'entity_autocomplete',
+ '#target_type' => 'user',
'#title' => t('Add user'),
'#maxlength' => 60,
'#size' => 40,
- '#autocomplete_path' => 'user/autocomplete',
+ '#selection_settings' => ['include_anonymous' => FALSE],
);
$form['add_button'] = array(
'#type' => 'button',
@@ -112,9 +114,22 @@ function _acl_edit_form_after_build($form, FormStateInterface $form_state) {
}
}
elseif (!empty($triggering_element['#value']) && $triggering_element['#value'] == $form['add_button']['#value'] && !empty($form['add']['#value'])) {
- $user = Database::getConnection()->query("SELECT u.uid, ud.name FROM {users} u INNER JOIN {users_field_data} ud ON u.uid = ud.uid WHERE ud.name = :name", [
- 'name' => $form['add']['#value'],
- ])->fetchObject();
+ $value = $form['add']['#value'];
+ $match = EntityAutocomplete::extractEntityIdFromAutocompleteInput($value);
+ if ($match === NULL) {
+ $user = \Drupal::database()
+ ->query("SELECT u.uid, ud.name FROM {users} u INNER JOIN {users_field_data} ud ON u.uid = ud.uid WHERE ud.name = :name", [
+ 'name' => $value,
+ ])
+ ->fetchObject();
+ }
+ else {
+ $user = \Drupal::database()
+ ->query("SELECT u.uid, ud.name FROM {users} u INNER JOIN {users_field_data} ud ON u.uid = ud.uid WHERE u.uid = :id", [
+ 'id' => $match,
+ ])
+ ->fetchObject();
+ }
if (!$user) {
$form_state->setError($form['add'], t("Invalid user specified."));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment