You are here

public function DatabaseTestForm::buildForm in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/system/tests/modules/database_test/src/Form/DatabaseTestForm.php \Drupal\database_test\Form\DatabaseTestForm::buildForm()
  2. 9 core/modules/system/tests/modules/database_test/src/Form/DatabaseTestForm.php \Drupal\database_test\Form\DatabaseTestForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

core/modules/system/tests/modules/database_test/src/Form/DatabaseTestForm.php, line 29

Class

DatabaseTestForm
Form controller for database_test module.

Namespace

Drupal\database_test\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $header = [
    'username' => [
      'data' => t('Username'),
      'field' => 'u.name',
    ],
    'status' => [
      'data' => t('Status'),
      'field' => 'u.status',
    ],
  ];
  $query = Database::getConnection()
    ->select('users_field_data', 'u');
  $query
    ->condition('u.uid', 0, '<>');
  $query
    ->condition('u.default_langcode', 1);
  $count_query = clone $query;
  $count_query
    ->addExpression('COUNT([u].[uid])');
  $query = $query
    ->extend(PagerSelectExtender::class)
    ->extend(TableSortExtender::class);
  $query
    ->fields('u', [
    'uid',
  ])
    ->limit(50)
    ->orderByHeader($header)
    ->setCountQuery($count_query);
  $uids = $query
    ->execute()
    ->fetchCol();
  $options = [];
  foreach (User::loadMultiple($uids) as $account) {
    $options[$account
      ->id()] = [
      'title' => [
        'data' => [
          '#title' => $account
            ->getAccountName(),
        ],
      ],
      'username' => $account
        ->getAccountName(),
      'status' => $account
        ->isActive() ? t('active') : t('blocked'),
    ];
  }
  $form['accounts'] = [
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    '#empty' => t('No people available.'),
  ];
  return $form;
}