5 use Drupal\Core\Cache\CacheableMetadata;
6 use Drupal\Core\Datetime\DateFormatterInterface;
7 use Drupal\Core\Entity\EntityInterface;
8 use Drupal\Core\Entity\EntityListBuilder;
9 use Drupal\Core\Entity\EntityStorageInterface;
10 use Drupal\Core\Entity\EntityTypeInterface;
11 use Drupal\Core\Routing\RedirectDestinationInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
15 * Defines a class to build a listing of user entities.
17 * @see \Drupal\user\Entity\User
19 class UserListBuilder extends EntityListBuilder {
22 * The date formatter service.
24 * @var \Drupal\Core\Datetime\DateFormatterInterface
26 protected $dateFormatter;
29 * The redirect destination service.
31 * @var \Drupal\Core\Routing\RedirectDestinationInterface
33 protected $redirectDestination;
36 * Constructs a new UserListBuilder object.
38 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
39 * The entity type definition.
40 * @param \Drupal\Core\Entity\EntityStorageInterface $storage
41 * The entity storage class.
42 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
43 * The date formatter service.
44 * @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination
45 * The redirect destination service.
47 public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter, RedirectDestinationInterface $redirect_destination) {
48 parent::__construct($entity_type, $storage);
49 $this->dateFormatter = $date_formatter;
50 $this->redirectDestination = $redirect_destination;
56 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
59 $container->get('entity.manager')->getStorage($entity_type->id()),
60 $container->get('date.formatter'),
61 $container->get('redirect.destination')
68 public function load() {
69 $entity_query = $this->storage->getQuery();
70 $entity_query->condition('uid', 0, '<>');
71 $entity_query->pager(50);
72 $header = $this->buildHeader();
73 $entity_query->tableSort($header);
74 $uids = $entity_query->execute();
75 return $this->storage->loadMultiple($uids);
81 public function buildHeader() {
84 'data' => $this->t('Username'),
86 'specifier' => 'name',
89 'data' => $this->t('Status'),
91 'specifier' => 'status',
92 'class' => [RESPONSIVE_PRIORITY_LOW],
95 'data' => $this->t('Roles'),
96 'class' => [RESPONSIVE_PRIORITY_LOW],
99 'data' => $this->t('Member for'),
100 'field' => 'created',
101 'specifier' => 'created',
103 'class' => [RESPONSIVE_PRIORITY_LOW],
106 'data' => $this->t('Last access'),
108 'specifier' => 'access',
109 'class' => [RESPONSIVE_PRIORITY_LOW],
112 return $header + parent::buildHeader();
118 public function buildRow(EntityInterface $entity) {
119 $row['username']['data'] = [
120 '#theme' => 'username',
121 '#account' => $entity,
123 $row['status'] = $entity->isActive() ? $this->t('active') : $this->t('blocked');
125 $roles = user_role_names(TRUE);
126 unset($roles[RoleInterface::AUTHENTICATED_ID]);
128 foreach ($entity->getRoles() as $role) {
129 if (isset($roles[$role])) {
130 $users_roles[] = $roles[$role];
134 $row['roles']['data'] = [
135 '#theme' => 'item_list',
136 '#items' => $users_roles,
139 'return_as_object' => TRUE,
141 $row['member_for']['data'] = $this->dateFormatter->formatTimeDiffSince($entity->getCreatedTime(), $options)->toRenderable();
142 $last_access = $this->dateFormatter->formatTimeDiffSince($entity->getLastAccessedTime(), $options);
144 if ($entity->getLastAccessedTime()) {
145 $row['access']['data']['#markup'] = $last_access->getString();
146 CacheableMetadata::createFromObject($last_access)->applyTo($row['access']['data']);
149 $row['access']['data']['#markup'] = t('never');
151 return $row + parent::buildRow($entity);
157 public function getOperations(EntityInterface $entity) {
158 $operations = parent::getOperations($entity);
159 if (isset($operations['edit'])) {
160 $destination = $this->redirectDestination->getAsArray();
161 $operations['edit']['query'] = $destination;
169 public function render() {
170 $build = parent::render();
171 $build['table']['#empty'] = $this->t('No people available.');