Chris@18
|
1 <?php
|
Chris@18
|
2
|
Chris@18
|
3 namespace Drupal\Core\Entity;
|
Chris@18
|
4
|
Chris@18
|
5 use Drupal\Core\Cache\Cache;
|
Chris@18
|
6 use Drupal\Core\Cache\RefinableCacheableDependencyTrait;
|
Chris@18
|
7 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
|
Chris@18
|
8 use Drupal\Core\Config\Entity\Exception\ConfigEntityIdLengthException;
|
Chris@18
|
9 use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException;
|
Chris@18
|
10 use Drupal\Core\Language\Language;
|
Chris@18
|
11 use Drupal\Core\Language\LanguageInterface;
|
Chris@18
|
12 use Drupal\Core\Link;
|
Chris@18
|
13 use Drupal\Core\Session\AccountInterface;
|
Chris@18
|
14 use Drupal\Core\Url;
|
Chris@18
|
15 use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
|
Chris@18
|
16 use Symfony\Component\Routing\Exception\RouteNotFoundException;
|
Chris@18
|
17
|
Chris@18
|
18 /**
|
Chris@18
|
19 * Defines a base entity class.
|
Chris@18
|
20 */
|
Chris@18
|
21 abstract class EntityBase implements EntityInterface {
|
Chris@18
|
22
|
Chris@18
|
23 use RefinableCacheableDependencyTrait;
|
Chris@18
|
24
|
Chris@18
|
25 use DependencySerializationTrait {
|
Chris@18
|
26 __sleep as traitSleep;
|
Chris@18
|
27 }
|
Chris@18
|
28
|
Chris@18
|
29 /**
|
Chris@18
|
30 * The entity type.
|
Chris@18
|
31 *
|
Chris@18
|
32 * @var string
|
Chris@18
|
33 */
|
Chris@18
|
34 protected $entityTypeId;
|
Chris@18
|
35
|
Chris@18
|
36 /**
|
Chris@18
|
37 * Boolean indicating whether the entity should be forced to be new.
|
Chris@18
|
38 *
|
Chris@18
|
39 * @var bool
|
Chris@18
|
40 */
|
Chris@18
|
41 protected $enforceIsNew;
|
Chris@18
|
42
|
Chris@18
|
43 /**
|
Chris@18
|
44 * A typed data object wrapping this entity.
|
Chris@18
|
45 *
|
Chris@18
|
46 * @var \Drupal\Core\TypedData\ComplexDataInterface
|
Chris@18
|
47 */
|
Chris@18
|
48 protected $typedData;
|
Chris@18
|
49
|
Chris@18
|
50 /**
|
Chris@18
|
51 * Constructs an Entity object.
|
Chris@18
|
52 *
|
Chris@18
|
53 * @param array $values
|
Chris@18
|
54 * An array of values to set, keyed by property name. If the entity type
|
Chris@18
|
55 * has bundles, the bundle key has to be specified.
|
Chris@18
|
56 * @param string $entity_type
|
Chris@18
|
57 * The type of the entity to create.
|
Chris@18
|
58 */
|
Chris@18
|
59 public function __construct(array $values, $entity_type) {
|
Chris@18
|
60 $this->entityTypeId = $entity_type;
|
Chris@18
|
61 // Set initial values.
|
Chris@18
|
62 foreach ($values as $key => $value) {
|
Chris@18
|
63 $this->$key = $value;
|
Chris@18
|
64 }
|
Chris@18
|
65 }
|
Chris@18
|
66
|
Chris@18
|
67 /**
|
Chris@18
|
68 * Gets the entity manager.
|
Chris@18
|
69 *
|
Chris@18
|
70 * @return \Drupal\Core\Entity\EntityManagerInterface
|
Chris@18
|
71 *
|
Chris@18
|
72 * @deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0.
|
Chris@18
|
73 * Use \Drupal::entityTypeManager() instead in most cases. If the needed
|
Chris@18
|
74 * method is not on \Drupal\Core\Entity\EntityTypeManagerInterface, see the
|
Chris@18
|
75 * deprecated \Drupal\Core\Entity\EntityManager to find the
|
Chris@18
|
76 * correct interface or service.
|
Chris@18
|
77 */
|
Chris@18
|
78 protected function entityManager() {
|
Chris@18
|
79 @trigger_error('Entity::getEntityManager() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use ::getEntityTypeManager() instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
|
Chris@18
|
80 return \Drupal::entityManager();
|
Chris@18
|
81 }
|
Chris@18
|
82
|
Chris@18
|
83 /**
|
Chris@18
|
84 * Gets the entity type manager.
|
Chris@18
|
85 *
|
Chris@18
|
86 * @return \Drupal\Core\Entity\EntityTypeManagerInterface
|
Chris@18
|
87 */
|
Chris@18
|
88 protected function entityTypeManager() {
|
Chris@18
|
89 return \Drupal::entityTypeManager();
|
Chris@18
|
90 }
|
Chris@18
|
91
|
Chris@18
|
92 /**
|
Chris@18
|
93 * Gets the entity type bundle info service.
|
Chris@18
|
94 *
|
Chris@18
|
95 * @return \Drupal\Core\Entity\EntityTypeBundleInfoInterface
|
Chris@18
|
96 */
|
Chris@18
|
97 protected function entityTypeBundleInfo() {
|
Chris@18
|
98 return \Drupal::service('entity_type.bundle.info');
|
Chris@18
|
99 }
|
Chris@18
|
100
|
Chris@18
|
101 /**
|
Chris@18
|
102 * Gets the language manager.
|
Chris@18
|
103 *
|
Chris@18
|
104 * @return \Drupal\Core\Language\LanguageManagerInterface
|
Chris@18
|
105 */
|
Chris@18
|
106 protected function languageManager() {
|
Chris@18
|
107 return \Drupal::languageManager();
|
Chris@18
|
108 }
|
Chris@18
|
109
|
Chris@18
|
110 /**
|
Chris@18
|
111 * Gets the UUID generator.
|
Chris@18
|
112 *
|
Chris@18
|
113 * @return \Drupal\Component\Uuid\UuidInterface
|
Chris@18
|
114 */
|
Chris@18
|
115 protected function uuidGenerator() {
|
Chris@18
|
116 return \Drupal::service('uuid');
|
Chris@18
|
117 }
|
Chris@18
|
118
|
Chris@18
|
119 /**
|
Chris@18
|
120 * {@inheritdoc}
|
Chris@18
|
121 */
|
Chris@18
|
122 public function id() {
|
Chris@18
|
123 return isset($this->id) ? $this->id : NULL;
|
Chris@18
|
124 }
|
Chris@18
|
125
|
Chris@18
|
126 /**
|
Chris@18
|
127 * {@inheritdoc}
|
Chris@18
|
128 */
|
Chris@18
|
129 public function uuid() {
|
Chris@18
|
130 return isset($this->uuid) ? $this->uuid : NULL;
|
Chris@18
|
131 }
|
Chris@18
|
132
|
Chris@18
|
133 /**
|
Chris@18
|
134 * {@inheritdoc}
|
Chris@18
|
135 */
|
Chris@18
|
136 public function isNew() {
|
Chris@18
|
137 return !empty($this->enforceIsNew) || !$this->id();
|
Chris@18
|
138 }
|
Chris@18
|
139
|
Chris@18
|
140 /**
|
Chris@18
|
141 * {@inheritdoc}
|
Chris@18
|
142 */
|
Chris@18
|
143 public function enforceIsNew($value = TRUE) {
|
Chris@18
|
144 $this->enforceIsNew = $value;
|
Chris@18
|
145
|
Chris@18
|
146 return $this;
|
Chris@18
|
147 }
|
Chris@18
|
148
|
Chris@18
|
149 /**
|
Chris@18
|
150 * {@inheritdoc}
|
Chris@18
|
151 */
|
Chris@18
|
152 public function getEntityTypeId() {
|
Chris@18
|
153 return $this->entityTypeId;
|
Chris@18
|
154 }
|
Chris@18
|
155
|
Chris@18
|
156 /**
|
Chris@18
|
157 * {@inheritdoc}
|
Chris@18
|
158 */
|
Chris@18
|
159 public function bundle() {
|
Chris@18
|
160 return $this->entityTypeId;
|
Chris@18
|
161 }
|
Chris@18
|
162
|
Chris@18
|
163 /**
|
Chris@18
|
164 * {@inheritdoc}
|
Chris@18
|
165 */
|
Chris@18
|
166 public function label() {
|
Chris@18
|
167 $label = NULL;
|
Chris@18
|
168 $entity_type = $this->getEntityType();
|
Chris@18
|
169 if (($label_callback = $entity_type->getLabelCallback()) && is_callable($label_callback)) {
|
Chris@18
|
170 $label = call_user_func($label_callback, $this);
|
Chris@18
|
171 }
|
Chris@18
|
172 elseif (($label_key = $entity_type->getKey('label')) && isset($this->{$label_key})) {
|
Chris@18
|
173 $label = $this->{$label_key};
|
Chris@18
|
174 }
|
Chris@18
|
175 return $label;
|
Chris@18
|
176 }
|
Chris@18
|
177
|
Chris@18
|
178 /**
|
Chris@18
|
179 * {@inheritdoc}
|
Chris@18
|
180 */
|
Chris@18
|
181 public function urlInfo($rel = 'canonical', array $options = []) {
|
Chris@18
|
182 @trigger_error('EntityInterface::urlInfo() is deprecated in Drupal 8.0.0 and will be removed in Drupal 9.0.0. EntityInterface::toUrl() instead. See https://www.drupal.org/node/2614344', E_USER_DEPRECATED);
|
Chris@18
|
183 return $this->toUrl($rel, $options);
|
Chris@18
|
184 }
|
Chris@18
|
185
|
Chris@18
|
186 /**
|
Chris@18
|
187 * {@inheritdoc}
|
Chris@18
|
188 */
|
Chris@18
|
189 public function toUrl($rel = 'canonical', array $options = []) {
|
Chris@18
|
190 if ($this->id() === NULL) {
|
Chris@18
|
191 throw new EntityMalformedException(sprintf('The "%s" entity cannot have a URI as it does not have an ID', $this->getEntityTypeId()));
|
Chris@18
|
192 }
|
Chris@18
|
193
|
Chris@18
|
194 // The links array might contain URI templates set in annotations.
|
Chris@18
|
195 $link_templates = $this->linkTemplates();
|
Chris@18
|
196
|
Chris@18
|
197 // Links pointing to the current revision point to the actual entity. So
|
Chris@18
|
198 // instead of using the 'revision' link, use the 'canonical' link.
|
Chris@18
|
199 if ($rel === 'revision' && $this instanceof RevisionableInterface && $this->isDefaultRevision()) {
|
Chris@18
|
200 $rel = 'canonical';
|
Chris@18
|
201 }
|
Chris@18
|
202
|
Chris@18
|
203 if (isset($link_templates[$rel])) {
|
Chris@18
|
204 $route_parameters = $this->urlRouteParameters($rel);
|
Chris@18
|
205 $route_name = "entity.{$this->entityTypeId}." . str_replace(['-', 'drupal:'], ['_', ''], $rel);
|
Chris@18
|
206 $uri = new Url($route_name, $route_parameters);
|
Chris@18
|
207 }
|
Chris@18
|
208 else {
|
Chris@18
|
209 $bundle = $this->bundle();
|
Chris@18
|
210 // A bundle-specific callback takes precedence over the generic one for
|
Chris@18
|
211 // the entity type.
|
Chris@18
|
212 $bundles = $this->entityTypeBundleInfo()->getBundleInfo($this->getEntityTypeId());
|
Chris@18
|
213 if (isset($bundles[$bundle]['uri_callback'])) {
|
Chris@18
|
214 $uri_callback = $bundles[$bundle]['uri_callback'];
|
Chris@18
|
215 }
|
Chris@18
|
216 elseif ($entity_uri_callback = $this->getEntityType()->getUriCallback()) {
|
Chris@18
|
217 $uri_callback = $entity_uri_callback;
|
Chris@18
|
218 }
|
Chris@18
|
219
|
Chris@18
|
220 // Invoke the callback to get the URI. If there is no callback, use the
|
Chris@18
|
221 // default URI format.
|
Chris@18
|
222 if (isset($uri_callback) && is_callable($uri_callback)) {
|
Chris@18
|
223 $uri = call_user_func($uri_callback, $this);
|
Chris@18
|
224 }
|
Chris@18
|
225 else {
|
Chris@18
|
226 throw new UndefinedLinkTemplateException("No link template '$rel' found for the '{$this->getEntityTypeId()}' entity type");
|
Chris@18
|
227 }
|
Chris@18
|
228 }
|
Chris@18
|
229
|
Chris@18
|
230 // Pass the entity data through as options, so that alter functions do not
|
Chris@18
|
231 // need to look up this entity again.
|
Chris@18
|
232 $uri
|
Chris@18
|
233 ->setOption('entity_type', $this->getEntityTypeId())
|
Chris@18
|
234 ->setOption('entity', $this);
|
Chris@18
|
235
|
Chris@18
|
236 // Display links by default based on the current language.
|
Chris@18
|
237 // Link relations that do not require an existing entity should not be
|
Chris@18
|
238 // affected by this entity's language, however.
|
Chris@18
|
239 if (!in_array($rel, ['collection', 'add-page', 'add-form'], TRUE)) {
|
Chris@18
|
240 $options += ['language' => $this->language()];
|
Chris@18
|
241 }
|
Chris@18
|
242
|
Chris@18
|
243 $uri_options = $uri->getOptions();
|
Chris@18
|
244 $uri_options += $options;
|
Chris@18
|
245
|
Chris@18
|
246 return $uri->setOptions($uri_options);
|
Chris@18
|
247 }
|
Chris@18
|
248
|
Chris@18
|
249 /**
|
Chris@18
|
250 * {@inheritdoc}
|
Chris@18
|
251 */
|
Chris@18
|
252 public function hasLinkTemplate($rel) {
|
Chris@18
|
253 $link_templates = $this->linkTemplates();
|
Chris@18
|
254 return isset($link_templates[$rel]);
|
Chris@18
|
255 }
|
Chris@18
|
256
|
Chris@18
|
257 /**
|
Chris@18
|
258 * Gets an array link templates.
|
Chris@18
|
259 *
|
Chris@18
|
260 * @return array
|
Chris@18
|
261 * An array of link templates containing paths.
|
Chris@18
|
262 */
|
Chris@18
|
263 protected function linkTemplates() {
|
Chris@18
|
264 return $this->getEntityType()->getLinkTemplates();
|
Chris@18
|
265 }
|
Chris@18
|
266
|
Chris@18
|
267 /**
|
Chris@18
|
268 * {@inheritdoc}
|
Chris@18
|
269 */
|
Chris@18
|
270 public function link($text = NULL, $rel = 'canonical', array $options = []) {
|
Chris@18
|
271 @trigger_error("EntityInterface::link() is deprecated in Drupal 8.0.0 and will be removed in Drupal 9.0.0. EntityInterface::toLink() instead. Note, the default relationship for configuration entities changes from 'edit-form' to 'canonical'. See https://www.drupal.org/node/2614344", E_USER_DEPRECATED);
|
Chris@18
|
272 return $this->toLink($text, $rel, $options)->toString();
|
Chris@18
|
273 }
|
Chris@18
|
274
|
Chris@18
|
275 /**
|
Chris@18
|
276 * {@inheritdoc}
|
Chris@18
|
277 */
|
Chris@18
|
278 public function toLink($text = NULL, $rel = 'canonical', array $options = []) {
|
Chris@18
|
279 if (!isset($text)) {
|
Chris@18
|
280 $text = $this->label();
|
Chris@18
|
281 }
|
Chris@18
|
282 $url = $this->toUrl($rel);
|
Chris@18
|
283 $options += $url->getOptions();
|
Chris@18
|
284 $url->setOptions($options);
|
Chris@18
|
285 return new Link($text, $url);
|
Chris@18
|
286 }
|
Chris@18
|
287
|
Chris@18
|
288 /**
|
Chris@18
|
289 * {@inheritdoc}
|
Chris@18
|
290 */
|
Chris@18
|
291 public function url($rel = 'canonical', $options = []) {
|
Chris@18
|
292 @trigger_error('EntityInterface::url() is deprecated in Drupal 8.0.0 and will be removed in Drupal 9.0.0. EntityInterface::toUrl() instead. Note, a \Drupal\Core\Url object is returned. See https://www.drupal.org/node/2614344', E_USER_DEPRECATED);
|
Chris@18
|
293 // While self::toUrl() will throw an exception if the entity has no id,
|
Chris@18
|
294 // the expected result for a URL is always a string.
|
Chris@18
|
295 if ($this->id() === NULL || !$this->hasLinkTemplate($rel)) {
|
Chris@18
|
296 return '';
|
Chris@18
|
297 }
|
Chris@18
|
298
|
Chris@18
|
299 $uri = $this->toUrl($rel);
|
Chris@18
|
300 $options += $uri->getOptions();
|
Chris@18
|
301 $uri->setOptions($options);
|
Chris@18
|
302 return $uri->toString();
|
Chris@18
|
303 }
|
Chris@18
|
304
|
Chris@18
|
305 /**
|
Chris@18
|
306 * Gets an array of placeholders for this entity.
|
Chris@18
|
307 *
|
Chris@18
|
308 * Individual entity classes may override this method to add additional
|
Chris@18
|
309 * placeholders if desired. If so, they should be sure to replicate the
|
Chris@18
|
310 * property caching logic.
|
Chris@18
|
311 *
|
Chris@18
|
312 * @param string $rel
|
Chris@18
|
313 * The link relationship type, for example: canonical or edit-form.
|
Chris@18
|
314 *
|
Chris@18
|
315 * @return array
|
Chris@18
|
316 * An array of URI placeholders.
|
Chris@18
|
317 */
|
Chris@18
|
318 protected function urlRouteParameters($rel) {
|
Chris@18
|
319 $uri_route_parameters = [];
|
Chris@18
|
320
|
Chris@18
|
321 if (!in_array($rel, ['collection', 'add-page', 'add-form'], TRUE)) {
|
Chris@18
|
322 // The entity ID is needed as a route parameter.
|
Chris@18
|
323 $uri_route_parameters[$this->getEntityTypeId()] = $this->id();
|
Chris@18
|
324 }
|
Chris@18
|
325 if ($rel === 'add-form' && ($this->getEntityType()->hasKey('bundle'))) {
|
Chris@18
|
326 $parameter_name = $this->getEntityType()->getBundleEntityType() ?: $this->getEntityType()->getKey('bundle');
|
Chris@18
|
327 $uri_route_parameters[$parameter_name] = $this->bundle();
|
Chris@18
|
328 }
|
Chris@18
|
329 if ($rel === 'revision' && $this instanceof RevisionableInterface) {
|
Chris@18
|
330 $uri_route_parameters[$this->getEntityTypeId() . '_revision'] = $this->getRevisionId();
|
Chris@18
|
331 }
|
Chris@18
|
332
|
Chris@18
|
333 return $uri_route_parameters;
|
Chris@18
|
334 }
|
Chris@18
|
335
|
Chris@18
|
336 /**
|
Chris@18
|
337 * {@inheritdoc}
|
Chris@18
|
338 */
|
Chris@18
|
339 public function uriRelationships() {
|
Chris@18
|
340 return array_filter(array_keys($this->linkTemplates()), function ($link_relation_type) {
|
Chris@18
|
341 // It's not guaranteed that every link relation type also has a
|
Chris@18
|
342 // corresponding route. For some, additional modules or configuration may
|
Chris@18
|
343 // be necessary. The interface demands that we only return supported URI
|
Chris@18
|
344 // relationships.
|
Chris@18
|
345 try {
|
Chris@18
|
346 $this->toUrl($link_relation_type)->toString(TRUE)->getGeneratedUrl();
|
Chris@18
|
347 }
|
Chris@18
|
348 catch (RouteNotFoundException $e) {
|
Chris@18
|
349 return FALSE;
|
Chris@18
|
350 }
|
Chris@18
|
351 catch (MissingMandatoryParametersException $e) {
|
Chris@18
|
352 return FALSE;
|
Chris@18
|
353 }
|
Chris@18
|
354 return TRUE;
|
Chris@18
|
355 });
|
Chris@18
|
356 }
|
Chris@18
|
357
|
Chris@18
|
358 /**
|
Chris@18
|
359 * {@inheritdoc}
|
Chris@18
|
360 */
|
Chris@18
|
361 public function access($operation, AccountInterface $account = NULL, $return_as_object = FALSE) {
|
Chris@18
|
362 if ($operation == 'create') {
|
Chris@18
|
363 return $this->entityTypeManager()
|
Chris@18
|
364 ->getAccessControlHandler($this->entityTypeId)
|
Chris@18
|
365 ->createAccess($this->bundle(), $account, [], $return_as_object);
|
Chris@18
|
366 }
|
Chris@18
|
367 return $this->entityTypeManager()
|
Chris@18
|
368 ->getAccessControlHandler($this->entityTypeId)
|
Chris@18
|
369 ->access($this, $operation, $account, $return_as_object);
|
Chris@18
|
370 }
|
Chris@18
|
371
|
Chris@18
|
372 /**
|
Chris@18
|
373 * {@inheritdoc}
|
Chris@18
|
374 */
|
Chris@18
|
375 public function language() {
|
Chris@18
|
376 if ($key = $this->getEntityType()->getKey('langcode')) {
|
Chris@18
|
377 $langcode = $this->$key;
|
Chris@18
|
378 $language = $this->languageManager()->getLanguage($langcode);
|
Chris@18
|
379 if ($language) {
|
Chris@18
|
380 return $language;
|
Chris@18
|
381 }
|
Chris@18
|
382 }
|
Chris@18
|
383 // Make sure we return a proper language object.
|
Chris@18
|
384 $langcode = !empty($this->langcode) ? $this->langcode : LanguageInterface::LANGCODE_NOT_SPECIFIED;
|
Chris@18
|
385 $language = new Language(['id' => $langcode]);
|
Chris@18
|
386 return $language;
|
Chris@18
|
387 }
|
Chris@18
|
388
|
Chris@18
|
389 /**
|
Chris@18
|
390 * {@inheritdoc}
|
Chris@18
|
391 */
|
Chris@18
|
392 public function save() {
|
Chris@18
|
393 $storage = $this->entityTypeManager()->getStorage($this->entityTypeId);
|
Chris@18
|
394 return $storage->save($this);
|
Chris@18
|
395 }
|
Chris@18
|
396
|
Chris@18
|
397 /**
|
Chris@18
|
398 * {@inheritdoc}
|
Chris@18
|
399 */
|
Chris@18
|
400 public function delete() {
|
Chris@18
|
401 if (!$this->isNew()) {
|
Chris@18
|
402 $this->entityTypeManager()->getStorage($this->entityTypeId)->delete([$this->id() => $this]);
|
Chris@18
|
403 }
|
Chris@18
|
404 }
|
Chris@18
|
405
|
Chris@18
|
406 /**
|
Chris@18
|
407 * {@inheritdoc}
|
Chris@18
|
408 */
|
Chris@18
|
409 public function createDuplicate() {
|
Chris@18
|
410 $duplicate = clone $this;
|
Chris@18
|
411 $entity_type = $this->getEntityType();
|
Chris@18
|
412 // Reset the entity ID and indicate that this is a new entity.
|
Chris@18
|
413 $duplicate->{$entity_type->getKey('id')} = NULL;
|
Chris@18
|
414 $duplicate->enforceIsNew();
|
Chris@18
|
415
|
Chris@18
|
416 // Check if the entity type supports UUIDs and generate a new one if so.
|
Chris@18
|
417 if ($entity_type->hasKey('uuid')) {
|
Chris@18
|
418 $duplicate->{$entity_type->getKey('uuid')} = $this->uuidGenerator()->generate();
|
Chris@18
|
419 }
|
Chris@18
|
420 return $duplicate;
|
Chris@18
|
421 }
|
Chris@18
|
422
|
Chris@18
|
423 /**
|
Chris@18
|
424 * {@inheritdoc}
|
Chris@18
|
425 */
|
Chris@18
|
426 public function getEntityType() {
|
Chris@18
|
427 return $this->entityTypeManager()->getDefinition($this->getEntityTypeId());
|
Chris@18
|
428 }
|
Chris@18
|
429
|
Chris@18
|
430 /**
|
Chris@18
|
431 * {@inheritdoc}
|
Chris@18
|
432 */
|
Chris@18
|
433 public function preSave(EntityStorageInterface $storage) {
|
Chris@18
|
434 // Check if this is an entity bundle.
|
Chris@18
|
435 if ($this->getEntityType()->getBundleOf()) {
|
Chris@18
|
436 // Throw an exception if the bundle ID is longer than 32 characters.
|
Chris@18
|
437 if (mb_strlen($this->id()) > EntityTypeInterface::BUNDLE_MAX_LENGTH) {
|
Chris@18
|
438 throw new ConfigEntityIdLengthException("Attempt to create a bundle with an ID longer than " . EntityTypeInterface::BUNDLE_MAX_LENGTH . " characters: $this->id().");
|
Chris@18
|
439 }
|
Chris@18
|
440 }
|
Chris@18
|
441 }
|
Chris@18
|
442
|
Chris@18
|
443 /**
|
Chris@18
|
444 * {@inheritdoc}
|
Chris@18
|
445 */
|
Chris@18
|
446 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
|
Chris@18
|
447 $this->invalidateTagsOnSave($update);
|
Chris@18
|
448 }
|
Chris@18
|
449
|
Chris@18
|
450 /**
|
Chris@18
|
451 * {@inheritdoc}
|
Chris@18
|
452 */
|
Chris@18
|
453 public static function preCreate(EntityStorageInterface $storage, array &$values) {
|
Chris@18
|
454 }
|
Chris@18
|
455
|
Chris@18
|
456 /**
|
Chris@18
|
457 * {@inheritdoc}
|
Chris@18
|
458 */
|
Chris@18
|
459 public function postCreate(EntityStorageInterface $storage) {
|
Chris@18
|
460 }
|
Chris@18
|
461
|
Chris@18
|
462 /**
|
Chris@18
|
463 * {@inheritdoc}
|
Chris@18
|
464 */
|
Chris@18
|
465 public static function preDelete(EntityStorageInterface $storage, array $entities) {
|
Chris@18
|
466 }
|
Chris@18
|
467
|
Chris@18
|
468 /**
|
Chris@18
|
469 * {@inheritdoc}
|
Chris@18
|
470 */
|
Chris@18
|
471 public static function postDelete(EntityStorageInterface $storage, array $entities) {
|
Chris@18
|
472 static::invalidateTagsOnDelete($storage->getEntityType(), $entities);
|
Chris@18
|
473 }
|
Chris@18
|
474
|
Chris@18
|
475 /**
|
Chris@18
|
476 * {@inheritdoc}
|
Chris@18
|
477 */
|
Chris@18
|
478 public static function postLoad(EntityStorageInterface $storage, array &$entities) {
|
Chris@18
|
479 }
|
Chris@18
|
480
|
Chris@18
|
481 /**
|
Chris@18
|
482 * {@inheritdoc}
|
Chris@18
|
483 */
|
Chris@18
|
484 public function referencedEntities() {
|
Chris@18
|
485 return [];
|
Chris@18
|
486 }
|
Chris@18
|
487
|
Chris@18
|
488 /**
|
Chris@18
|
489 * {@inheritdoc}
|
Chris@18
|
490 */
|
Chris@18
|
491 public function getCacheContexts() {
|
Chris@18
|
492 return $this->cacheContexts;
|
Chris@18
|
493 }
|
Chris@18
|
494
|
Chris@18
|
495 /**
|
Chris@18
|
496 * {@inheritdoc}
|
Chris@18
|
497 */
|
Chris@18
|
498 public function getCacheTagsToInvalidate() {
|
Chris@18
|
499 // @todo Add bundle-specific listing cache tag?
|
Chris@18
|
500 // https://www.drupal.org/node/2145751
|
Chris@18
|
501 if ($this->isNew()) {
|
Chris@18
|
502 return [];
|
Chris@18
|
503 }
|
Chris@18
|
504 return [$this->entityTypeId . ':' . $this->id()];
|
Chris@18
|
505 }
|
Chris@18
|
506
|
Chris@18
|
507 /**
|
Chris@18
|
508 * {@inheritdoc}
|
Chris@18
|
509 */
|
Chris@18
|
510 public function getCacheTags() {
|
Chris@18
|
511 if ($this->cacheTags) {
|
Chris@18
|
512 return Cache::mergeTags($this->getCacheTagsToInvalidate(), $this->cacheTags);
|
Chris@18
|
513 }
|
Chris@18
|
514 return $this->getCacheTagsToInvalidate();
|
Chris@18
|
515 }
|
Chris@18
|
516
|
Chris@18
|
517 /**
|
Chris@18
|
518 * {@inheritdoc}
|
Chris@18
|
519 */
|
Chris@18
|
520 public function getCacheMaxAge() {
|
Chris@18
|
521 return $this->cacheMaxAge;
|
Chris@18
|
522 }
|
Chris@18
|
523
|
Chris@18
|
524 /**
|
Chris@18
|
525 * {@inheritdoc}
|
Chris@18
|
526 */
|
Chris@18
|
527 public static function load($id) {
|
Chris@18
|
528 $entity_type_repository = \Drupal::service('entity_type.repository');
|
Chris@18
|
529 $entity_type_manager = \Drupal::entityTypeManager();
|
Chris@18
|
530 $storage = $entity_type_manager->getStorage($entity_type_repository->getEntityTypeFromClass(get_called_class()));
|
Chris@18
|
531 return $storage->load($id);
|
Chris@18
|
532 }
|
Chris@18
|
533
|
Chris@18
|
534 /**
|
Chris@18
|
535 * {@inheritdoc}
|
Chris@18
|
536 */
|
Chris@18
|
537 public static function loadMultiple(array $ids = NULL) {
|
Chris@18
|
538 $entity_type_repository = \Drupal::service('entity_type.repository');
|
Chris@18
|
539 $entity_type_manager = \Drupal::entityTypeManager();
|
Chris@18
|
540 $storage = $entity_type_manager->getStorage($entity_type_repository->getEntityTypeFromClass(get_called_class()));
|
Chris@18
|
541 return $storage->loadMultiple($ids);
|
Chris@18
|
542 }
|
Chris@18
|
543
|
Chris@18
|
544 /**
|
Chris@18
|
545 * {@inheritdoc}
|
Chris@18
|
546 */
|
Chris@18
|
547 public static function create(array $values = []) {
|
Chris@18
|
548 $entity_type_repository = \Drupal::service('entity_type.repository');
|
Chris@18
|
549 $entity_type_manager = \Drupal::entityTypeManager();
|
Chris@18
|
550 $storage = $entity_type_manager->getStorage($entity_type_repository->getEntityTypeFromClass(get_called_class()));
|
Chris@18
|
551 return $storage->create($values);
|
Chris@18
|
552 }
|
Chris@18
|
553
|
Chris@18
|
554 /**
|
Chris@18
|
555 * Invalidates an entity's cache tags upon save.
|
Chris@18
|
556 *
|
Chris@18
|
557 * @param bool $update
|
Chris@18
|
558 * TRUE if the entity has been updated, or FALSE if it has been inserted.
|
Chris@18
|
559 */
|
Chris@18
|
560 protected function invalidateTagsOnSave($update) {
|
Chris@18
|
561 // An entity was created or updated: invalidate its list cache tags. (An
|
Chris@18
|
562 // updated entity may start to appear in a listing because it now meets that
|
Chris@18
|
563 // listing's filtering requirements. A newly created entity may start to
|
Chris@18
|
564 // appear in listings because it did not exist before.)
|
Chris@18
|
565 $tags = $this->getEntityType()->getListCacheTags();
|
Chris@18
|
566 if ($this->hasLinkTemplate('canonical')) {
|
Chris@18
|
567 // Creating or updating an entity may change a cached 403 or 404 response.
|
Chris@18
|
568 $tags = Cache::mergeTags($tags, ['4xx-response']);
|
Chris@18
|
569 }
|
Chris@18
|
570 if ($update) {
|
Chris@18
|
571 // An existing entity was updated, also invalidate its unique cache tag.
|
Chris@18
|
572 $tags = Cache::mergeTags($tags, $this->getCacheTagsToInvalidate());
|
Chris@18
|
573 }
|
Chris@18
|
574 Cache::invalidateTags($tags);
|
Chris@18
|
575 }
|
Chris@18
|
576
|
Chris@18
|
577 /**
|
Chris@18
|
578 * Invalidates an entity's cache tags upon delete.
|
Chris@18
|
579 *
|
Chris@18
|
580 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
|
Chris@18
|
581 * The entity type definition.
|
Chris@18
|
582 * @param \Drupal\Core\Entity\EntityInterface[] $entities
|
Chris@18
|
583 * An array of entities.
|
Chris@18
|
584 */
|
Chris@18
|
585 protected static function invalidateTagsOnDelete(EntityTypeInterface $entity_type, array $entities) {
|
Chris@18
|
586 $tags = $entity_type->getListCacheTags();
|
Chris@18
|
587 foreach ($entities as $entity) {
|
Chris@18
|
588 // An entity was deleted: invalidate its own cache tag, but also its list
|
Chris@18
|
589 // cache tags. (A deleted entity may cause changes in a paged list on
|
Chris@18
|
590 // other pages than the one it's on. The one it's on is handled by its own
|
Chris@18
|
591 // cache tag, but subsequent list pages would not be invalidated, hence we
|
Chris@18
|
592 // must invalidate its list cache tags as well.)
|
Chris@18
|
593 $tags = Cache::mergeTags($tags, $entity->getCacheTagsToInvalidate());
|
Chris@18
|
594 }
|
Chris@18
|
595 Cache::invalidateTags($tags);
|
Chris@18
|
596 }
|
Chris@18
|
597
|
Chris@18
|
598 /**
|
Chris@18
|
599 * {@inheritdoc}
|
Chris@18
|
600 */
|
Chris@18
|
601 public function getOriginalId() {
|
Chris@18
|
602 // By default, entities do not support renames and do not have original IDs.
|
Chris@18
|
603 return NULL;
|
Chris@18
|
604 }
|
Chris@18
|
605
|
Chris@18
|
606 /**
|
Chris@18
|
607 * {@inheritdoc}
|
Chris@18
|
608 */
|
Chris@18
|
609 public function setOriginalId($id) {
|
Chris@18
|
610 // By default, entities do not support renames and do not have original IDs.
|
Chris@18
|
611 // If the specified ID is anything except NULL, this should mark this entity
|
Chris@18
|
612 // as no longer new.
|
Chris@18
|
613 if ($id !== NULL) {
|
Chris@18
|
614 $this->enforceIsNew(FALSE);
|
Chris@18
|
615 }
|
Chris@18
|
616
|
Chris@18
|
617 return $this;
|
Chris@18
|
618 }
|
Chris@18
|
619
|
Chris@18
|
620 /**
|
Chris@18
|
621 * {@inheritdoc}
|
Chris@18
|
622 */
|
Chris@18
|
623 public function toArray() {
|
Chris@18
|
624 return [];
|
Chris@18
|
625 }
|
Chris@18
|
626
|
Chris@18
|
627 /**
|
Chris@18
|
628 * {@inheritdoc}
|
Chris@18
|
629 */
|
Chris@18
|
630 public function getTypedData() {
|
Chris@18
|
631 if (!isset($this->typedData)) {
|
Chris@18
|
632 $class = \Drupal::typedDataManager()->getDefinition('entity')['class'];
|
Chris@18
|
633 $this->typedData = $class::createFromEntity($this);
|
Chris@18
|
634 }
|
Chris@18
|
635 return $this->typedData;
|
Chris@18
|
636 }
|
Chris@18
|
637
|
Chris@18
|
638 /**
|
Chris@18
|
639 * {@inheritdoc}
|
Chris@18
|
640 */
|
Chris@18
|
641 public function __sleep() {
|
Chris@18
|
642 $this->typedData = NULL;
|
Chris@18
|
643 return $this->traitSleep();
|
Chris@18
|
644 }
|
Chris@18
|
645
|
Chris@18
|
646 /**
|
Chris@18
|
647 * {@inheritdoc}
|
Chris@18
|
648 */
|
Chris@18
|
649 public function getConfigDependencyKey() {
|
Chris@18
|
650 return $this->getEntityType()->getConfigDependencyKey();
|
Chris@18
|
651 }
|
Chris@18
|
652
|
Chris@18
|
653 /**
|
Chris@18
|
654 * {@inheritdoc}
|
Chris@18
|
655 */
|
Chris@18
|
656 public function getConfigDependencyName() {
|
Chris@18
|
657 return $this->getEntityTypeId() . ':' . $this->bundle() . ':' . $this->uuid();
|
Chris@18
|
658 }
|
Chris@18
|
659
|
Chris@18
|
660 /**
|
Chris@18
|
661 * {@inheritdoc}
|
Chris@18
|
662 */
|
Chris@18
|
663 public function getConfigTarget() {
|
Chris@18
|
664 // For content entities, use the UUID for the config target identifier.
|
Chris@18
|
665 // This ensures that references to the target can be deployed reliably.
|
Chris@18
|
666 return $this->uuid();
|
Chris@18
|
667 }
|
Chris@18
|
668
|
Chris@18
|
669 }
|