Chris@18
|
1 <?php
|
Chris@18
|
2
|
Chris@18
|
3 namespace Drupal\user;
|
Chris@18
|
4
|
Chris@18
|
5 use Drupal\Core\Entity\EntityTypeInterface;
|
Chris@18
|
6 use Drupal\Core\Entity\Exception\UnsupportedEntityTypeDefinitionException;
|
Chris@18
|
7 use Drupal\Core\Field\BaseFieldDefinition;
|
Chris@18
|
8 use Drupal\Core\StringTranslation\TranslatableMarkup;
|
Chris@18
|
9
|
Chris@18
|
10 /**
|
Chris@18
|
11 * Provides a trait for entities that have an owner.
|
Chris@18
|
12 */
|
Chris@18
|
13 trait EntityOwnerTrait {
|
Chris@18
|
14
|
Chris@18
|
15 /**
|
Chris@18
|
16 * Returns an array of base field definitions for entity owners.
|
Chris@18
|
17 *
|
Chris@18
|
18 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
|
Chris@18
|
19 * The entity type to add the owner field to.
|
Chris@18
|
20 *
|
Chris@18
|
21 * @return \Drupal\Core\Field\BaseFieldDefinition[]
|
Chris@18
|
22 * An array of base field definitions.
|
Chris@18
|
23 *
|
Chris@18
|
24 * @throws \Drupal\Core\Entity\Exception\UnsupportedEntityTypeDefinitionException
|
Chris@18
|
25 * Thrown when the entity type does not implement EntityOwnerInterface or
|
Chris@18
|
26 * if it does not have an "owner" entity key.
|
Chris@18
|
27 */
|
Chris@18
|
28 public static function ownerBaseFieldDefinitions(EntityTypeInterface $entity_type) {
|
Chris@18
|
29 if (!is_subclass_of($entity_type->getClass(), EntityOwnerInterface::class)) {
|
Chris@18
|
30 throw new UnsupportedEntityTypeDefinitionException('The entity type ' . $entity_type->id() . ' does not implement \Drupal\user\EntityOwnerInterface.');
|
Chris@18
|
31 }
|
Chris@18
|
32 if (!$entity_type->hasKey('owner')) {
|
Chris@18
|
33 throw new UnsupportedEntityTypeDefinitionException('The entity type ' . $entity_type->id() . ' does not have an "owner" entity key.');
|
Chris@18
|
34 }
|
Chris@18
|
35
|
Chris@18
|
36 return [
|
Chris@18
|
37 $entity_type->getKey('owner') => BaseFieldDefinition::create('entity_reference')
|
Chris@18
|
38 ->setLabel(new TranslatableMarkup('User ID'))
|
Chris@18
|
39 ->setSetting('target_type', 'user')
|
Chris@18
|
40 ->setTranslatable($entity_type->isTranslatable())
|
Chris@18
|
41 ->setDefaultValueCallback(static::class . '::getDefaultEntityOwner'),
|
Chris@18
|
42 ];
|
Chris@18
|
43 }
|
Chris@18
|
44
|
Chris@18
|
45 /**
|
Chris@18
|
46 * {@inheritdoc}
|
Chris@18
|
47 */
|
Chris@18
|
48 public function getOwnerId() {
|
Chris@18
|
49 return $this->getEntityKey('owner');
|
Chris@18
|
50 }
|
Chris@18
|
51
|
Chris@18
|
52 /**
|
Chris@18
|
53 * {@inheritdoc}
|
Chris@18
|
54 */
|
Chris@18
|
55 public function setOwnerId($uid) {
|
Chris@18
|
56 $key = $this->getEntityType()->getKey('owner');
|
Chris@18
|
57 $this->set($key, $uid);
|
Chris@18
|
58
|
Chris@18
|
59 return $this;
|
Chris@18
|
60 }
|
Chris@18
|
61
|
Chris@18
|
62 /**
|
Chris@18
|
63 * {@inheritdoc}
|
Chris@18
|
64 */
|
Chris@18
|
65 public function getOwner() {
|
Chris@18
|
66 $key = $this->getEntityType()->getKey('owner');
|
Chris@18
|
67 return $this->get($key)->entity;
|
Chris@18
|
68 }
|
Chris@18
|
69
|
Chris@18
|
70 /**
|
Chris@18
|
71 * {@inheritdoc}
|
Chris@18
|
72 */
|
Chris@18
|
73 public function setOwner(UserInterface $account) {
|
Chris@18
|
74 $key = $this->getEntityType()->getKey('owner');
|
Chris@18
|
75 $this->set($key, $account);
|
Chris@18
|
76
|
Chris@18
|
77 return $this;
|
Chris@18
|
78 }
|
Chris@18
|
79
|
Chris@18
|
80 /**
|
Chris@18
|
81 * Default value callback for 'owner' base field.
|
Chris@18
|
82 *
|
Chris@18
|
83 * @return mixed
|
Chris@18
|
84 * A default value for the owner field.
|
Chris@18
|
85 */
|
Chris@18
|
86 public static function getDefaultEntityOwner() {
|
Chris@18
|
87 return \Drupal::currentUser()->id();
|
Chris@18
|
88 }
|
Chris@18
|
89
|
Chris@18
|
90 }
|