3 namespace Drupal\entity_module_test\Entity;
5 use Drupal\Core\Entity\EntityPublishedInterface;
6 use Drupal\Core\Entity\EntityPublishedTrait;
7 use Drupal\Core\Entity\EntityTypeInterface;
8 use Drupal\Core\Field\BaseFieldDefinition;
9 use Drupal\entity\Revision\RevisionableContentEntityBase;
10 use Drupal\user\EntityOwnerInterface;
11 use Drupal\user\UserInterface;
14 * Provides a test entity which uses all the capabilities of entity module.
17 * id = "entity_test_enhanced_with_owner",
18 * label = @Translation("Enhanced entity with owner"),
19 * label_collection = @Translation("Enhanced entities with owner"),
20 * label_singular = @Translation("enhanced entity with owner"),
21 * label_plural = @Translation("enhanced entities with owner"),
22 * label_count = @PluralTranslation(
23 * singular = "@count enhanced entity with owner",
24 * plural = "@count enhanced entities with owner",
27 * "storage" = "\Drupal\Core\Entity\Sql\SqlContentEntityStorage",
28 * "access" = "\Drupal\entity\UncacheableEntityAccessControlHandler",
29 * "query_access" = "\Drupal\entity\QueryAccess\UncacheableQueryAccessHandler",
30 * "permission_provider" = "\Drupal\entity\UncacheableEntityPermissionProvider",
32 * "add" = "\Drupal\entity\Form\RevisionableContentEntityForm",
33 * "edit" = "\Drupal\entity\Form\RevisionableContentEntityForm",
34 * "delete" = "\Drupal\Core\Entity\EntityDeleteForm",
36 * "route_provider" = {
37 * "html" = "\Drupal\entity\Routing\DefaultHtmlRouteProvider",
38 * "revision" = "\Drupal\entity\Routing\RevisionRouteProvider",
39 * "delete-multiple" = "\Drupal\entity\Routing\DeleteMultipleRouteProvider",
41 * "local_action_provider" = {
42 * "collection" = "\Drupal\entity\Menu\EntityCollectionLocalActionProvider",
44 * "list_builder" = "\Drupal\Core\Entity\EntityListBuilder",
45 * "views_data" = "\Drupal\views\EntityViewsData",
47 * base_table = "entity_test_enhanced_with_owner",
48 * data_table = "entity_test_enhanced_with_owner_field_data",
49 * revision_table = "entity_test_enhanced_with_owner_revision",
50 * revision_data_table = "entity_test_enhanced_with_owner_field_revision",
51 * translatable = TRUE,
52 * revisionable = TRUE,
53 * admin_permission = "administer entity_test_enhanced_with_owner",
54 * permission_granularity = "bundle",
59 * "langcode" = "langcode",
62 * "published" = "status",
65 * "add-page" = "/entity_test_enhanced_with_owner/add",
66 * "add-form" = "/entity_test_enhanced_with_owner/add/{type}",
67 * "edit-form" = "/entity_test_enhanced_with_owner/{entity_test_enhanced_with_owner}/edit",
68 * "canonical" = "/entity_test_enhanced_with_owner/{entity_test_enhanced_with_owner}",
69 * "collection" = "/entity_test_enhanced_with_owner",
73 class EnhancedEntityWithOwner extends RevisionableContentEntityBase implements EntityOwnerInterface, EntityPublishedInterface {
75 use EntityPublishedTrait;
80 public function getOwner() {
81 return $this->get('user_id')->entity;
87 public function getOwnerId() {
88 return $this->get('user_id')->target_id;
94 public function setOwnerId($uid) {
95 $this->set('user_id', $uid);
102 public function setOwner(UserInterface $account) {
103 $this->set('user_id', $account->id());
110 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
111 $fields = parent::baseFieldDefinitions($entity_type);
112 $fields += static::publishedBaseFieldDefinitions($entity_type);
114 $fields['name'] = BaseFieldDefinition::create('string')
116 ->setRevisionable(TRUE)
117 ->setDisplayOptions('view', [
123 $fields['user_id'] = BaseFieldDefinition::create('entity_reference')
124 ->setLabel(t('User ID'))
125 ->setDescription(t('The ID of the associated user.'))
126 ->setSetting('target_type', 'user')
127 ->setSetting('handler', 'default')
128 // Default EntityTest entities to have the root user as the owner, to
130 ->setDefaultValue([0 => ['target_id' => 1]])
131 ->setTranslatable(TRUE)
132 ->setDisplayOptions('form', [
133 'type' => 'entity_reference_autocomplete',
136 'match_operator' => 'CONTAINS',