Mercurial > hg > isophonics-drupal-site
comparison core/modules/user/src/UserListBuilder.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | af1871eacc83 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\user; | |
4 | |
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; | |
13 | |
14 /** | |
15 * Defines a class to build a listing of user entities. | |
16 * | |
17 * @see \Drupal\user\Entity\User | |
18 */ | |
19 class UserListBuilder extends EntityListBuilder { | |
20 | |
21 /** | |
22 * The date formatter service. | |
23 * | |
24 * @var \Drupal\Core\Datetime\DateFormatterInterface | |
25 */ | |
26 protected $dateFormatter; | |
27 | |
28 /** | |
29 * The redirect destination service. | |
30 * | |
31 * @var \Drupal\Core\Routing\RedirectDestinationInterface | |
32 */ | |
33 protected $redirectDestination; | |
34 | |
35 /** | |
36 * Constructs a new UserListBuilder object. | |
37 * | |
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. | |
46 */ | |
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; | |
51 } | |
52 | |
53 /** | |
54 * {@inheritdoc} | |
55 */ | |
56 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { | |
57 return new static( | |
58 $entity_type, | |
59 $container->get('entity.manager')->getStorage($entity_type->id()), | |
60 $container->get('date.formatter'), | |
61 $container->get('redirect.destination') | |
62 ); | |
63 } | |
64 | |
65 /** | |
66 * {@inheritdoc} | |
67 */ | |
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); | |
76 } | |
77 | |
78 /** | |
79 * {@inheritdoc} | |
80 */ | |
81 public function buildHeader() { | |
82 $header = [ | |
83 'username' => [ | |
84 'data' => $this->t('Username'), | |
85 'field' => 'name', | |
86 'specifier' => 'name', | |
87 ], | |
88 'status' => [ | |
89 'data' => $this->t('Status'), | |
90 'field' => 'status', | |
91 'specifier' => 'status', | |
92 'class' => [RESPONSIVE_PRIORITY_LOW], | |
93 ], | |
94 'roles' => [ | |
95 'data' => $this->t('Roles'), | |
96 'class' => [RESPONSIVE_PRIORITY_LOW], | |
97 ], | |
98 'member_for' => [ | |
99 'data' => $this->t('Member for'), | |
100 'field' => 'created', | |
101 'specifier' => 'created', | |
102 'sort' => 'desc', | |
103 'class' => [RESPONSIVE_PRIORITY_LOW], | |
104 ], | |
105 'access' => [ | |
106 'data' => $this->t('Last access'), | |
107 'field' => 'access', | |
108 'specifier' => 'access', | |
109 'class' => [RESPONSIVE_PRIORITY_LOW], | |
110 ], | |
111 ]; | |
112 return $header + parent::buildHeader(); | |
113 } | |
114 | |
115 /** | |
116 * {@inheritdoc} | |
117 */ | |
118 public function buildRow(EntityInterface $entity) { | |
119 $row['username']['data'] = [ | |
120 '#theme' => 'username', | |
121 '#account' => $entity, | |
122 ]; | |
123 $row['status'] = $entity->isActive() ? $this->t('active') : $this->t('blocked'); | |
124 | |
125 $roles = user_role_names(TRUE); | |
126 unset($roles[RoleInterface::AUTHENTICATED_ID]); | |
127 $users_roles = []; | |
128 foreach ($entity->getRoles() as $role) { | |
129 if (isset($roles[$role])) { | |
130 $users_roles[] = $roles[$role]; | |
131 } | |
132 } | |
133 asort($users_roles); | |
134 $row['roles']['data'] = [ | |
135 '#theme' => 'item_list', | |
136 '#items' => $users_roles, | |
137 ]; | |
138 $options = [ | |
139 'return_as_object' => TRUE, | |
140 ]; | |
141 $row['member_for']['data'] = $this->dateFormatter->formatTimeDiffSince($entity->getCreatedTime(), $options)->toRenderable(); | |
142 $last_access = $this->dateFormatter->formatTimeDiffSince($entity->getLastAccessedTime(), $options); | |
143 | |
144 if ($entity->getLastAccessedTime()) { | |
145 $row['access']['data']['#markup'] = $last_access->getString(); | |
146 CacheableMetadata::createFromObject($last_access)->applyTo($row['access']['data']); | |
147 } | |
148 else { | |
149 $row['access']['data']['#markup'] = t('never'); | |
150 } | |
151 return $row + parent::buildRow($entity); | |
152 } | |
153 | |
154 /** | |
155 * {@inheritdoc} | |
156 */ | |
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; | |
162 } | |
163 return $operations; | |
164 } | |
165 | |
166 /** | |
167 * {@inheritdoc} | |
168 */ | |
169 public function render() { | |
170 $build = parent::render(); | |
171 $build['table']['#empty'] = $this->t('No people available.'); | |
172 return $build; | |
173 } | |
174 | |
175 } |