function UserListBuilder::buildRow

Same name in other branches
  1. 9 core/modules/user/src/UserListBuilder.php \Drupal\user\UserListBuilder::buildRow()
  2. 8.9.x core/modules/user/src/UserListBuilder.php \Drupal\user\UserListBuilder::buildRow()
  3. 11.x core/modules/user/src/UserListBuilder.php \Drupal\user\UserListBuilder::buildRow()

Overrides EntityListBuilder::buildRow

File

core/modules/user/src/UserListBuilder.php, line 120

Class

UserListBuilder
Defines a class to build a listing of user entities.

Namespace

Drupal\user

Code

public function buildRow(EntityInterface $entity) {
    $row['username']['data'] = [
        '#theme' => 'username',
        '#account' => $entity,
    ];
    $row['status'] = $entity->isActive() ? $this->t('active') : $this->t('blocked');
    $roles = Role::loadMultiple($entity->getRoles());
    unset($roles[RoleInterface::ANONYMOUS_ID]);
    unset($roles[RoleInterface::AUTHENTICATED_ID]);
    $users_roles = array_map(fn(RoleInterface $role) => $role->label(), $roles);
    asort($users_roles);
    $row['roles']['data'] = [
        '#theme' => 'item_list',
        '#items' => $users_roles,
    ];
    $options = [
        'return_as_object' => TRUE,
    ];
    $row['member_for']['data'] = $this->dateFormatter
        ->formatTimeDiffSince($entity->getCreatedTime(), $options)
        ->toRenderable();
    $last_access = $this->dateFormatter
        ->formatTimeDiffSince($entity->getLastAccessedTime(), $options);
    if ($entity->getLastAccessedTime()) {
        $row['access']['data']['#markup'] = $last_access->getString();
        CacheableMetadata::createFromObject($last_access)->applyTo($row['access']['data']);
    }
    else {
        $row['access']['data']['#markup'] = t('never');
    }
    return $row + parent::buildRow($entity);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.