DatabaseTestForm.php 1.8 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\database_test\Form\DatabaseTestForm.
 */

namespace Drupal\database_test\Form;

10 11 12
use Drupal\Component\Utility\String;
use Drupal\Core\Form\FormBase;

13
/**
14
 * Form controller for database_test module.
15
 */
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
class DatabaseTestForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'database_test_theme_tablesort';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, array &$form_state) {
    $header = array(
      'username' => array('data' => t('Username'), 'field' => 'u.name'),
      'status' => array('data' => t('Status'), 'field' => 'u.status'),
    );

    $query = db_select('users', 'u');
    $query->condition('u.uid', 0, '<>');

    $count_query = clone $query;
    $count_query->addExpression('COUNT(u.uid)');

    $query = $query
      ->extend('Drupal\Core\Database\Query\PagerSelectExtender')
      ->extend('Drupal\Core\Database\Query\TableSortExtender');
    $query
      ->fields('u', array('uid'))
      ->limit(50)
      ->orderByHeader($header)
      ->setCountQuery($count_query);
    $uids = $query
      ->execute()
      ->fetchCol();

    $options = array();

    foreach (user_load_multiple($uids) as $account) {
      $options[$account->id()] = array(
        'title' => array('data' => array('#title' => String::checkPlain($account->getUsername()))),
        'username' => String::checkPlain($account->getUsername()),
        'status' =>  $account->isActive() ? t('active') : t('blocked'),
      );
    }

    $form['accounts'] = array(
      '#type' => 'tableselect',
      '#header' => $header,
      '#options' => $options,
      '#empty' => t('No people available.'),
    );

    return $form;
  }
71 72

  /**
73
   * {@inheritdoc}
74
   */
75
  public function submitForm(array &$form, array &$form_state) {
76 77 78
  }

}