8fbb0234524ec6b5ceeb2d9f2ead1d57c3ae3946
[yaffs-website] / web / modules / contrib / devel / src / Form / SwitchUserForm.php
1 <?php
2
3 namespace Drupal\devel\Form;
4
5 use Drupal\Core\Access\CsrfTokenGenerator;
6 use Drupal\Core\Form\FormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Url;
9 use Drupal\user\Entity\User;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Defines a form that allows privileged users to generate entities.
14  */
15 class SwitchUserForm extends FormBase {
16
17   /**
18    * The csrf token generator.
19    *
20    * @var \Drupal\Core\Access\CsrfTokenGenerator
21    */
22   protected $csrfToken;
23
24   /**
25    * Constructs a new SwitchUserForm object.
26    *
27    * @param \Drupal\Core\Access\CsrfTokenGenerator $csrf_token_generator
28    */
29   public function __construct(CsrfTokenGenerator $csrf_token_generator) {
30     $this->csrfToken = $csrf_token_generator;
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public static function create(ContainerInterface $container) {
37     return new static(
38       $container->get('csrf_token')
39     );
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function getFormId() {
46     return 'devel_switchuser_form';
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public function buildForm(array $form, FormStateInterface $form_state) {
53     $form['autocomplete'] = [
54       '#type' => 'container',
55       '#attributes' => [
56         'class' => ['container-inline'],
57       ],
58     ];
59     $form['autocomplete']['userid'] = [
60       '#type' => 'entity_autocomplete',
61       '#title' => $this->t('Username'),
62       '#placeholder' => $this->t('Enter username'),
63       '#target_type' => 'user',
64       '#selection_settings' => [
65         'include_anonymous' => FALSE
66       ],
67       '#process_default_value' => FALSE,
68       '#maxlength' => USERNAME_MAX_LENGTH,
69       '#title_display' => 'invisible',
70       '#required' => TRUE,
71       '#size' => '28',
72     ];
73
74     $form['autocomplete']['actions'] = ['#type' => 'actions'];
75     $form['autocomplete']['actions']['submit'] = [
76       '#type' => 'submit',
77       '#value' => $this->t('Switch'),
78     ];
79
80     return $form;
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function validateForm(array &$form, FormStateInterface $form_state) {
87     if (!$account = User::load($form_state->getValue('userid'))) {
88       $form_state->setErrorByName('userid', $this->t('Username not found'));
89     }
90     else {
91       $form_state->setValue('username', $account->getAccountName());
92     }
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   public function submitForm(array &$form, FormStateInterface $form_state) {
99     // We cannot rely on automatic token creation, since the csrf seed changes
100     // after the redirect and the generated token is not more valid.
101     // TODO find another way to do this.
102     $url = Url::fromRoute('devel.switch', ['name' => $form_state->getValue('username')]);
103     $url->setOption('query', ['token' => $this->csrfToken->get($url->getInternalPath())]);
104
105     $form_state->setRedirectUrl($url);
106   }
107
108 }