You are here

trait EntityOwnerTrait in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/user/src/EntityOwnerTrait.php \Drupal\user\EntityOwnerTrait

Provides a trait for entities that have an owner.

Hierarchy

6 files declare their use of EntityOwnerTrait
Comment.php in core/modules/comment/src/Entity/Comment.php
ContentModerationState.php in core/modules/content_moderation/src/Entity/ContentModerationState.php
File.php in core/modules/file/src/Entity/File.php
Media.php in core/modules/media/src/Entity/Media.php
Node.php in core/modules/node/src/Entity/Node.php

... See full list

File

core/modules/user/src/EntityOwnerTrait.php, line 13

Namespace

Drupal\user
View source
trait EntityOwnerTrait {

  /**
   * Returns an array of base field definitions for entity owners.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type to add the owner field to.
   *
   * @return \Drupal\Core\Field\BaseFieldDefinition[]
   *   An array of base field definitions.
   *
   * @throws \Drupal\Core\Entity\Exception\UnsupportedEntityTypeDefinitionException
   *   Thrown when the entity type does not implement EntityOwnerInterface or
   *   if it does not have an "owner" entity key.
   */
  public static function ownerBaseFieldDefinitions(EntityTypeInterface $entity_type) {
    if (!is_subclass_of($entity_type
      ->getClass(), EntityOwnerInterface::class)) {
      throw new UnsupportedEntityTypeDefinitionException('The entity type ' . $entity_type
        ->id() . ' does not implement \\Drupal\\user\\EntityOwnerInterface.');
    }
    if (!$entity_type
      ->hasKey('owner')) {
      throw new UnsupportedEntityTypeDefinitionException('The entity type ' . $entity_type
        ->id() . ' does not have an "owner" entity key.');
    }
    return [
      $entity_type
        ->getKey('owner') => BaseFieldDefinition::create('entity_reference')
        ->setLabel(new TranslatableMarkup('User ID'))
        ->setSetting('target_type', 'user')
        ->setTranslatable($entity_type
        ->isTranslatable())
        ->setDefaultValueCallback(static::class . '::getDefaultEntityOwner'),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getOwnerId() {
    return $this
      ->getEntityKey('owner');
  }

  /**
   * {@inheritdoc}
   */
  public function setOwnerId($uid) {
    $key = $this
      ->getEntityType()
      ->getKey('owner');
    $this
      ->set($key, $uid);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getOwner() {
    $key = $this
      ->getEntityType()
      ->getKey('owner');
    return $this
      ->get($key)->entity;
  }

  /**
   * {@inheritdoc}
   */
  public function setOwner(UserInterface $account) {
    $key = $this
      ->getEntityType()
      ->getKey('owner');
    $this
      ->set($key, $account);
    return $this;
  }

  /**
   * Default value callback for 'owner' base field.
   *
   * @return mixed
   *   A default value for the owner field.
   */
  public static function getDefaultEntityOwner() {
    return \Drupal::currentUser()
      ->id();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityOwnerTrait::getDefaultEntityOwner public static function Default value callback for 'owner' base field. 1
EntityOwnerTrait::getOwner public function 1
EntityOwnerTrait::getOwnerId public function
EntityOwnerTrait::ownerBaseFieldDefinitions public static function Returns an array of base field definitions for entity owners.
EntityOwnerTrait::setOwner public function
EntityOwnerTrait::setOwnerId public function