12eebccab638a4d9cb8bc31768034c1b60b8b3e8
[yaffs-website] / web / core / modules / system / tests / modules / database_test / src / Form / DatabaseTestForm.php
1 <?php
2
3 namespace Drupal\database_test\Form;
4
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\user\Entity\User;
8
9 /**
10  * Form controller for database_test module.
11  */
12 class DatabaseTestForm extends FormBase {
13
14   /**
15    * {@inheritdoc}
16    */
17   public function getFormId() {
18     return 'database_test_theme_tablesort';
19   }
20
21   /**
22    * {@inheritdoc}
23    */
24   public function buildForm(array $form, FormStateInterface $form_state) {
25     $header = [
26       'username' => ['data' => t('Username'), 'field' => 'u.name'],
27       'status' => ['data' => t('Status'), 'field' => 'u.status'],
28     ];
29
30     $query = db_select('users_field_data', 'u');
31     $query->condition('u.uid', 0, '<>');
32     $query->condition('u.default_langcode', 1);
33
34     $count_query = clone $query;
35     $count_query->addExpression('COUNT(u.uid)');
36
37     $query = $query
38       ->extend('Drupal\Core\Database\Query\PagerSelectExtender')
39       ->extend('Drupal\Core\Database\Query\TableSortExtender');
40     $query
41       ->fields('u', ['uid'])
42       ->limit(50)
43       ->orderByHeader($header)
44       ->setCountQuery($count_query);
45     $uids = $query
46       ->execute()
47       ->fetchCol();
48
49     $options = [];
50
51     foreach (User::loadMultiple($uids) as $account) {
52       $options[$account->id()] = [
53         'title' => ['data' => ['#title' => $account->getUsername()]],
54         'username' => $account->getUsername(),
55         'status' => $account->isActive() ? t('active') : t('blocked'),
56       ];
57     }
58
59     $form['accounts'] = [
60       '#type' => 'tableselect',
61       '#header' => $header,
62       '#options' => $options,
63       '#empty' => t('No people available.'),
64     ];
65
66     return $form;
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function submitForm(array &$form, FormStateInterface $form_state) {
73   }
74
75 }