public static function EntityOwnerTrait::ownerBaseFieldDefinitions in Profile 8
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.
1 call to EntityOwnerTrait::ownerBaseFieldDefinitions()
- Profile::baseFieldDefinitions in src/
Entity/ Profile.php - Provides base field definitions for an entity type.
File
- src/
EntityOwnerTrait.php, line 33
Class
- EntityOwnerTrait
- Provides a trait for entities that have an owner.
Namespace
Drupal\profileCode
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'),
];
}