This repository has been archived by the owner on Jan 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
EntityHelperTrait.php
306 lines (276 loc) · 9.4 KB
/
EntityHelperTrait.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
/**
* @file
* Contains Drupal\entity_embed\EntityHelperTrait.
*/
namespace Drupal\entity_embed;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager;
/**
* Wrapper methods for entity loading and rendering.
*
* This utility trait should only be used in application-level code, such as
* classes that would implement ContainerInjectionInterface. Services registered
* in the Container should not use this trait but inject the appropriate service
* directly for easier testing.
*/
trait EntityHelperTrait {
/**
* The entity manager service.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* The module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface.
*/
protected $moduleHandler;
/**
* The display plugin manager.
*
* @var \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager.
*/
protected $displayPluginManager;
/**
* Loads an entity from the database.
*
* @param string $entity_type
* The entity type to load, e.g. node or user.
* @param mixed $id
* The id or UUID of the entity to load.
*
* @return \Drupal\Core\Entity\EntityInterface
* The entity object, or NULL if there is no entity with the given id or
* UUID.
*/
protected function loadEntity($entity_type, $id) {
$entities = $this->loadMultipleEntities($entity_type, array($id));
return !empty($entities) ? reset($entities) : NULL;
}
/**
* Loads multiple entities from the database.
*
* @param string $entity_type
* The entity type to load, e.g. node or user.
* @param array $ids
* An array of entity IDs or UUIDs.
*
* @return array
* An array of entity objects indexed by their ids.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
protected function loadMultipleEntities($entity_type, array $ids) {
$entities = array();
$storage = $this->entityManager()->getStorage($entity_type);
$uuids = array_filter($ids, 'Drupal\Component\Uuid\Uuid::isValid');
if (!empty($uuids)) {
$definition = $this->entityManager()->getDefinition($entity_type);
if (!$uuid_key = $definition->getKey('uuid')) {
throw new EntityStorageException("Entity type $entity_type does not support UUIDs.");
}
$entities += $storage->loadByProperties(array($uuid_key => $uuids));
}
if ($remaining_ids = array_diff($ids, $uuids)) {
$entities += $storage->loadMultiple($remaining_ids);
}
return $entities;
}
/**
* Determines if an entity can be rendered.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity object.
*
* @return bool
* TRUE if the entity's type has a view builder controller, otherwise FALSE.
*/
protected function canRenderEntity(EntityInterface $entity) {
$entity_type = $entity->getEntityTypeId();
return $this->entityManager()->hasController($entity_type, 'view_builder');
}
/**
* Returns the render array for an entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to be rendered.
* @param string $view_mode
* The view mode that should be used to display the entity.
* @param string $langcode
* (optional) For which language the entity should be rendered, defaults to
* the current content language.
*
* @return array
* A render array for the entity.
*/
protected function renderEntity(EntityInterface $entity, $view_mode, $langcode = NULL) {
$render_controller = $this->entityManager()->getViewBuilder($entity->getEntityTypeId());
return $render_controller->view($entity, $view_mode, $langcode);
}
/**
* Renders an entity using an EntityEmbedDisplay plugin.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to be rendered.
* @param string $plugin_id
* The EntityEmbedDisplay plugin ID.
* @param array $plugin_configuration
* (optional) Array of plugin configuration values.
* @param array $context
* (optional) Array of additional context values, usually the embed HTML
* tag's attributes.
*
* @return string
* The HTML of the entity rendered with the display plugin.
*
* @throws \Drupal\entity_embed\RecursiveRenderingException;
*/
protected function renderEntityEmbedDisplayPlugin(EntityInterface $entity, $plugin_id, array $plugin_configuration = array(), array $context = array()) {
// Protect ourselves from recursive rendering.
static $depth = 0;
$depth++;
if ($depth > 20) {
throw new RecursiveRenderingException(format_string('Recursive rendering detected when rendering entity @entity_type(@entity_id). Aborting rendering.', array('@entity_type' => $entity->getEntityTypeId(), '@entity_id' => $entity->id())));
}
// Allow modules to alter the entity prior to display rendering.
$this->moduleHandler()->invokeAll('entity_preembed', array($entity, $context));
// Build the display plugin.
$display = $this->displayPluginManager()->createInstance($plugin_id, $plugin_configuration);
$display->setContextValue('entity', $entity);
$display->setAttributes($context);
// Check if the display plugin is accessible. This also checks entity
// access, which is why we never call $entity->access() here.
if (!$display->access()) {
return '';
}
// Build and render the display plugin, allowing modules to alter the
// result before rendering.
$build = $display->build();
$this->moduleHandler()->alter('entity_embed', $build, $display);
$entity_output = drupal_render($build);
$depth--;
return $entity_output;
}
/**
* Check access to an entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity object.
* @param string $operation
* (optional) The operation to be performed. Defaults to view.
* @param \Drupal\Core\Session\AccountInterface $account
* (optional) The user for which to check access, or NULL to check access
* for the current user. Defaults to NULL.
*
* @return bool|null
* self::ALLOW, self::DENY, or self::KILL.
*/
protected function accessEntity(EntityInterface $entity, $op = 'view', AccountInterface $account = NULL) {
switch ($entity->getEntityTypeId()) {
case 'file':
// Due to issues with access checking with file entities in core,
// we cannot actually use Entity::access() which would have been called
// by parent::access().
//
// @see https://drupal.org/node/2128791
// @see https://drupal.org/node/2148353
// @see https://drupal.org/node/2078473
switch (file_uri_scheme($entity->getFileUri())) {
case 'public':
return TRUE;
case 'private':
case 'temporary':
$headers = $this->moduleHandler()->invokeAll('file_download', array($uri));
foreach ($headers as $result) {
if ($result == -1) {
return FALSE;
}
}
if (count($headers)) {
return TRUE;
}
break;
}
default:
return $entity->access($op, $account);
}
}
/**
* Returns the entity manager.
*
* @return \Drupal\Core\Entity\EntityManagerInterface
* The entity manager.
*/
protected function entityManager() {
if (!isset($this->entityManager)) {
$this->entityManager = \Drupal::entityManager();
}
return $this->entityManager;
}
/**
* Sets the entity manager service.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entityManager
* The entity manager service.
*
* @return self
*/
public function setEntityManager(EntityManagerInterface $entityManager) {
$this->entityManager = $entityManager;
return $this;
}
/**
* Returns the module handler.
*
* @return \Drupal\Core\Extension\ModuleHandlerInterface
* The module handler.
*/
protected function moduleHandler() {
if (!isset($this->moduleHandler)) {
$this->moduleHandler = \Drupal::moduleHandler();
}
return $this->moduleHandler;
}
/**
* Sets the module handler service.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* The module handler service.
*
* @return self
*/
public function setModuleHandler(ModuleHandlerInterface $moduleHandler) {
$this->moduleHandler = $moduleHandler;
return $this;
}
/**
* Returns the display plugin manager.
*
* @return \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager
* The display plugin manager.
*/
protected function displayPluginManager() {
if (!isset($this->displayPluginManager)) {
$this->displayPluginManager = \Drupal::service('plugin.manager.entity_embed.display');
}
return $this->displayPluginManager;
}
/**
* Sets the display plugin manager service.
*
* @param \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager $displayPluginManager
* The display plugin manager service.
*
* @return self
*/
public function setDisplayPluginManager(EntityEmbedDisplayManager $displayPluginManager) {
$this->displayPluginManager = $displayPluginManager;
return $this;
}
}