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