You are here

public function UserSwitchController::userSwitchList in User Switch 8

Returns a simple page.

Return value

array A simple renderable array.

1 string reference to 'UserSwitchController::userSwitchList'
userswitch.routing.yml in ./userswitch.routing.yml
userswitch.routing.yml

File

src/Controller/UserSwitchController.php, line 52

Class

UserSwitchController
Provides route responses for the Example module.

Namespace

Drupal\userswitch\Controller

Code

public function userSwitchList() {
  $_uid = $this->currentUser
    ->id();
  $query = $this->database
    ->select('users_field_data', 'u');
  $query
    ->fields('u', [
    'uid',
    'name',
    'mail',
  ]);

  // For the pagination we need to extend the pagerselectextender and
  // limit in the query.
  $query
    ->condition('uid', $_uid, '!=');
  $pager = $query
    ->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
    ->limit(10);
  $results = $pager
    ->execute()
    ->fetchAll();

  // Initialize an empty array.
  $output = [];
  $header = [
    '#',
    'Name',
    'Mail',
    'Operations',
  ];

  // Next, loop through the $results array.
  foreach ($results as $result) {
    if ($result->uid != 0) {
      $url = Url::fromUri('internal:/admin/people/user/' . $result->uid);
      $_link = Link::fromTextAndUrl($this
        ->t('Click Here'), $url);
      $output[$result->uid] = [
        'userid' => $result->uid,
        'Username' => $result->name,
        'email' => $result->mail,
        'link' => $_link,
      ];
    }
  }
  $element[] = [
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $output,
  ];
  return $element;
}