-
Notifications
You must be signed in to change notification settings - Fork 134
/
OgRoleListBuilder.php
165 lines (141 loc) · 4.24 KB
/
OgRoleListBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
namespace Drupal\og\Entity;
use Drupal\Core\Url;
use Drupal\Core\Config\Entity\DraggableListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\og\Og;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Defines a class to build a listing of og_role entities.
*
* @see \Drupal\og\Entity\OgRole
*/
class OgRoleListBuilder extends DraggableListBuilder {
/**
* The group entity type.
*
* @var string
*/
protected $group_type;
/**
* The group entity bundle id.
*
* @var string
*/
protected $group_bundle;
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static(
$entity_type,
$container->get('entity.manager')->getStorage($entity_type->id()),
$container->get('current_route_match')
);
}
/**
* Constructs a new OgRoleListBuilder object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
* @param \Drupal\Core\Entity\EntityStorageInterface $storage
* The entity storage class.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route matcher.
*/
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, RouteMatchInterface $route_match) {
parent::__construct($entity_type, $storage);
// When on the default og_role list route,
//we should have a group entity type and bundle.
if ($route_match->getRouteName() == 'entity.og_role.collection') {
$parameters = $route_match->getParameters();
// Check if the route had a group type parameter.
if ($parameters->has('entity_type_id')) {
$this->group_type = $parameters->get('entity_type_id');
}
if ($parameters->has('bundle')) {
$this->group_bundle = $parameters->get('bundle');
}
}
}
/**
* {@inheritdoc}
*/
protected function getEntityIds() {
$query = $this->getStorage()->getQuery()
->condition('group_type', $this->group_type, '=')
->condition('group_bundle', $this->group_bundle, '=')
->sort($this->entityType->getKey('weight'));
// Only add the pager if a limit is specified.
if ($this->limit) {
$query->pager($this->limit);
}
return array_values($query->execute());
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'og_roles_admin';
}
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['label'] = t('Name');
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
$row['label'] = $entity->label();
return $row + parent::buildRow($entity);
}
/**
* {@inheritdoc}
*/
public function getDefaultOperations(EntityInterface $role) {
$operations = parent::getDefaultOperations($role);
// @TODO if ($entity->hasLinkTemplate('edit-permissions-form')).
$operations['permissions'] = [
'title' => t('Edit permissions'),
'weight' => 20,
'url' => Url::fromRoute('og_ui.permissions_edit_form', [
'entity_type' => $this->group_type,
'bundle' => $this->group_bundle,
'og_role' => $role->id(),
]),
];
if ($role->isLocked()) {
if (isset($operations['edit'])) {
unset($operations['edit']);
}
if (isset($operations['delete'])) {
unset($operations['delete']);
}
}
return $operations;
}
/**
* {@inheritdoc}
*/
public function render() {
// Return a 404 error when this is not a group.
if (!Og::isGroup($this->group_type, $this->group_bundle)) {
throw new NotFoundHttpException();
}
$build = parent::render();
$build['entities']['#empty'] = $this->t('There are no OG roles available yet. <a href="@link">Add an OG role</a>.', [
'@link' => Url::fromRoute('entity.og_role.add_form', [
'entity_type' => $this->group_type,
'bundle' => $this->group_bundle,
])->toString()
]);
return $build;
}
}