You are here

trait EntityOwnerTrait in CRM Core 8.2

Same name and namespace in other branches
  1. 8 src/EntityOwnerTrait.php \Drupal\crm_core\EntityOwnerTrait

Trait implementing the various methods defined in EntityOwnerInterface.

@todo Remove once the same trait is in Core.

Hierarchy

See also

\Drupal\user\EntityOwnerInterface

3 files declare their use of EntityOwnerTrait
Activity.php in modules/crm_core_activity/src/Entity/Activity.php
Individual.php in modules/crm_core_contact/src/Entity/Individual.php
Organization.php in modules/crm_core_contact/src/Entity/Organization.php

File

src/EntityOwnerTrait.php, line 15

Namespace

Drupal\crm_core
View source
trait EntityOwnerTrait {

  /**
   * Defines 'uid' base field definition.
   *
   * @return \Drupal\Core\Field\BaseFieldDefinition
   *   A field definition object.
   */
  public static function getOwnerFieldDefinition() {
    return BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Owner'))
      ->setRevisionable(TRUE)
      ->setSetting('target_type', 'user')
      ->setDefaultValueCallback('Drupal\\crm_core\\EntityOwnerTrait::getDefaultAuthorId');
  }

  /**
   * Default value callback for 'uid' base field definition.
   *
   * @see ::baseFieldDefinitions()
   *
   * @return int
   *   The user ID.
   */
  public static function getDefaultAuthorId() {
    return \Drupal::currentUser()
      ->id();
  }

  /**
   * Returns the entity owner's user entity.
   *
   * @return \Drupal\user\UserInterface
   *   The owner user entity.
   */
  public function getOwner() {
    return $this
      ->get('uid')->entity;
  }

  /**
   * Sets the entity owner's user entity.
   *
   * @param \Drupal\user\UserInterface $account
   *   The owner user entity.
   *
   * @return $this
   */
  public function setOwner(UserInterface $account) {
    $this
      ->set('uid', $account
      ->id());
    return $this;
  }

  /**
   * Returns the entity owner's user ID.
   *
   * @return int|null
   *   The owner user ID, or NULL in case the user ID field has not been set on
   *   the entity.
   */
  public function getOwnerId() {
    return $this
      ->get('uid')->target_id;
  }

  /**
   * Sets the entity owner's user ID.
   *
   * @param int $uid
   *   The owner user id.
   *
   * @return $this
   */
  public function setOwnerId($uid) {
    $this
      ->set('uid', $uid);
    return $this;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityOwnerTrait::getDefaultAuthorId public static function Default value callback for 'uid' base field definition.
EntityOwnerTrait::getOwner public function Returns the entity owner's user entity.
EntityOwnerTrait::getOwnerFieldDefinition public static function Defines 'uid' base field definition.
EntityOwnerTrait::getOwnerId public function Returns the entity owner's user ID.
EntityOwnerTrait::setOwner public function Sets the entity owner's user entity.
EntityOwnerTrait::setOwnerId public function Sets the entity owner's user ID.