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