function SwitchUserListHelper::buildUserList

Builds the user listing as renderable array.

Parameters

\Drupal\Core\Session\AccountInterface[] $accounts: The accounts to be rendered in the list.

Return value

array A renderable array.

File

src/SwitchUserListHelper.php, line 147

Class

SwitchUserListHelper
Switch user helper service.

Namespace

Drupal\devel

Code

public function buildUserList(array $accounts) : array {
    $links = [];
    foreach ($accounts as $account) {
        $links[$account->id()] = [
            'title' => $account->getDisplayName(),
            'url' => Url::fromRoute('devel.switch', [
                'name' => $account->getAccountName(),
            ]),
            'query' => $this->redirectDestination
                ->getAsArray(),
            'attributes' => [
                'title' => $account->hasPermission('switch users') ? $this->t('This user can switch back.') : $this->t('Caution: this user will be unable to switch back.'),
            ],
        ];
        if ($account->isAnonymous()) {
            $links[$account->id()]['url'] = Url::fromRoute('user.logout');
        }
        if ($this->currentUser
            ->id() === $account->id()) {
            $links[$account->id()]['title'] = new FormattableMarkup('<strong>%user</strong>', [
                '%user' => $account->getDisplayName(),
            ]);
        }
    }
    return [
        '#theme' => 'links',
        '#links' => $links,
        '#attached' => [
            'library' => [
                'devel/devel',
            ],
        ],
    ];
}