function EntityOwnerTrait::ownerBaseFieldDefinitions

Same name and namespace in other branches
  1. 9 core/modules/user/src/EntityOwnerTrait.php \Drupal\user\EntityOwnerTrait::ownerBaseFieldDefinitions()
  2. 10 core/modules/user/src/EntityOwnerTrait.php \Drupal\user\EntityOwnerTrait::ownerBaseFieldDefinitions()
  3. 11.x core/modules/user/src/EntityOwnerTrait.php \Drupal\user\EntityOwnerTrait::ownerBaseFieldDefinitions()

Returns an array of base field definitions for entity owners.

Parameters

\Drupal\Core\Entity\EntityTypeInterface $entity_type: The entity type to add the owner field to.

Return value

\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.

6 calls to EntityOwnerTrait::ownerBaseFieldDefinitions()
Comment::baseFieldDefinitions in core/modules/comment/src/Entity/Comment.php
ContentModerationState::baseFieldDefinitions in core/modules/content_moderation/src/Entity/ContentModerationState.php
File::baseFieldDefinitions in core/modules/file/src/Entity/File.php
Media::baseFieldDefinitions in core/modules/media/src/Entity/Media.php
Node::baseFieldDefinitions in core/modules/node/src/Entity/Node.php

... See full list

File

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

Class

EntityOwnerTrait
Provides a trait for entities that have an owner.

Namespace

Drupal\user

Code

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'),
    ];
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.