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