Chris@5: getClass(), EntityOwnerInterface::class)) { Chris@5: throw new UnsupportedEntityTypeDefinitionException('The entity type ' . $entity_type->id() . ' does not implement \Drupal\user\EntityOwnerInterface.'); Chris@5: } Chris@5: if (!$entity_type->hasKey('owner')) { Chris@5: throw new UnsupportedEntityTypeDefinitionException('The entity type ' . $entity_type->id() . ' does not have an "owner" entity key.'); Chris@5: } Chris@5: Chris@5: return [ Chris@5: $entity_type->getKey('owner') => BaseFieldDefinition::create('entity_reference') Chris@5: ->setLabel(new TranslatableMarkup('User ID')) Chris@5: ->setSetting('target_type', 'user') Chris@5: ->setTranslatable($entity_type->isTranslatable()) Chris@5: ->setDefaultValueCallback(static::class . '::getDefaultEntityOwner'), Chris@5: ]; Chris@5: } Chris@5: Chris@5: /** Chris@5: * {@inheritdoc} Chris@5: */ Chris@5: public function getOwnerId() { Chris@5: return $this->getEntityKey('owner'); Chris@5: } Chris@5: Chris@5: /** Chris@5: * {@inheritdoc} Chris@5: */ Chris@5: public function setOwnerId($uid) { Chris@5: $key = $this->getEntityType()->getKey('owner'); Chris@5: $this->set($key, $uid); Chris@5: Chris@5: return $this; Chris@5: } Chris@5: Chris@5: /** Chris@5: * {@inheritdoc} Chris@5: */ Chris@5: public function getOwner() { Chris@5: $key = $this->getEntityType()->getKey('owner'); Chris@5: return $this->get($key)->entity; Chris@5: } Chris@5: Chris@5: /** Chris@5: * {@inheritdoc} Chris@5: */ Chris@5: public function setOwner(UserInterface $account) { Chris@5: $key = $this->getEntityType()->getKey('owner'); Chris@5: $this->set($key, $account); Chris@5: Chris@5: return $this; Chris@5: } Chris@5: Chris@5: /** Chris@5: * Default value callback for 'owner' base field. Chris@5: * Chris@5: * @return mixed Chris@5: * A default value for the owner field. Chris@5: */ Chris@5: public static function getDefaultEntityOwner() { Chris@5: return \Drupal::currentUser()->id(); Chris@5: } Chris@5: Chris@5: }