From 9f26dd1c07259ee43685f4122ddb21ca8f486931 Mon Sep 17 00:00:00 2001
From: Rajashree Datar <rdatar@faichi.com>
Date: Thu, 28 Jan 2016 10:52:16 +0530
Subject: [PATCH] Issue #929250 : Allow user id input

---
 src/Form/MasqueradeForm.php | 48 ++++++++++++++++++++++++---------------------
 1 file changed, 26 insertions(+), 22 deletions(-)

diff --git a/src/Form/MasqueradeForm.php b/src/Form/MasqueradeForm.php
index 05850e4..3332e23 100644
--- a/src/Form/MasqueradeForm.php
+++ b/src/Form/MasqueradeForm.php
@@ -66,22 +66,17 @@ class MasqueradeForm extends FormBase {
    * {@inheritdoc}
    */
   public function buildForm(array $form, FormStateInterface $form_state) {
-    $form['autocomplete'] = array(
-      '#type' => 'container',
-      '#attributes' => array('class' => array('container-inline')),
-    );
-    $form['autocomplete']['masquerade_as'] = array(
-      '#type' => 'entity_autocomplete',
-      '#target_type' => 'user',
-      '#selection_settings' => ['include_anonymous' => FALSE, 'match_operator' => 'STARTS_WITH'],
-      '#title' => $this->t('Username'),
-      '#title_display' => 'invisible',
+
+    $form['masquerade_as'] = array(
+      '#type' => 'textfield',
+      '#title' => $this->t('Username or user id'),
       '#required' => TRUE,
       '#placeholder' => $this->t('Masquerade as…'),
       '#size' => '18',
     );
-    $form['autocomplete']['actions'] = array('#type' => 'actions');
-    $form['autocomplete']['actions']['submit'] = array(
+
+    $form['actions'] = array('#type' => 'actions');
+    $form['actions']['submit'] = array(
       '#type' => 'submit',
       '#value' => $this->t('Switch'),
     );
@@ -92,20 +87,29 @@ class MasqueradeForm extends FormBase {
    * {@inheritdoc}
    */
   public function validateForm(array &$form, FormStateInterface $form_state) {
-    $user_id = $form_state->getValue('masquerade_as');
-    if (empty($user_id)) {
-      $form_state->setErrorByName('masquerade_as', $this->t('The user does not exist. Please enter a valid username.'));
-      return;
+    $masq_user = $form_state->getValue('masquerade_as');
+
+    $target_account = user_load_by_name($masq_user);
+    if (empty($target_account)) {
+      // If user cannot be loaded by name, try id
+      $target_account = $this->entityManager
+        ->getStorage('user')
+        ->load($masq_user);
     }
-    $target_account = $this->entityManager
-      ->getStorage('user')
-      ->load($user_id);
-    if ($error = masquerade_switch_user_validate($target_account)) {
-      $form_state->setErrorByName('masquerade_as', $error);
+    // Either the user id or the user name loads a valid user.
+    if (!empty($target_account)) {
+      if ($error = masquerade_switch_user_validate($target_account)) {
+        $form_state->setErrorByName('masquerade_as', $error);
+      }
+      else {
+        $form_state->setValue('masquerade_target_account', $target_account);
+      }
     }
     else {
-      $form_state->setValue('masquerade_target_account', $target_account);
+      $form_state->setErrorByName('masquerade_as', $this->t('The user does not exist. Please enter a valid username or user id.'));
+      return;
     }
+
   }
 
   /**
-- 
1.9.1

