Added Entity and Entity Reference Revisions which got dropped somewhere along the...
[yaffs-website] / web / modules / contrib / entity / tests / modules / entity_module_test / src / Entity / EnhancedEntityWithOwner.php
1 <?php
2
3 namespace Drupal\entity_module_test\Entity;
4
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;
12
13 /**
14  * Provides a test entity which uses all the capabilities of entity module.
15  *
16  * @ContentEntityType(
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",
25  *   ),
26  *   handlers = {
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",
31  *     "form" = {
32  *       "add" = "\Drupal\entity\Form\RevisionableContentEntityForm",
33  *       "edit" = "\Drupal\entity\Form\RevisionableContentEntityForm",
34  *       "delete" = "\Drupal\Core\Entity\EntityDeleteForm",
35  *     },
36  *     "route_provider" = {
37  *       "html" = "\Drupal\entity\Routing\DefaultHtmlRouteProvider",
38  *       "revision" = "\Drupal\entity\Routing\RevisionRouteProvider",
39  *       "delete-multiple" = "\Drupal\entity\Routing\DeleteMultipleRouteProvider",
40  *     },
41  *     "local_action_provider" = {
42  *       "collection" = "\Drupal\entity\Menu\EntityCollectionLocalActionProvider",
43  *     },
44  *     "list_builder" = "\Drupal\Core\Entity\EntityListBuilder",
45  *     "views_data" = "\Drupal\views\EntityViewsData",
46  *   },
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",
55  *   entity_keys = {
56  *     "id" = "id",
57  *     "bundle" = "type",
58  *     "revision" = "vid",
59  *     "langcode" = "langcode",
60  *     "label" = "name",
61  *     "uid" = "user_id",
62  *     "published" = "status",
63  *   },
64  *   links = {
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",
70  *   },
71  * )
72  */
73 class EnhancedEntityWithOwner extends RevisionableContentEntityBase implements EntityOwnerInterface, EntityPublishedInterface {
74
75   use EntityPublishedTrait;
76
77   /**
78    * {@inheritdoc}
79    */
80   public function getOwner() {
81     return $this->get('user_id')->entity;
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public function getOwnerId() {
88     return $this->get('user_id')->target_id;
89   }
90
91   /**
92    * {@inheritdoc}
93    */
94   public function setOwnerId($uid) {
95     $this->set('user_id', $uid);
96     return $this;
97   }
98
99   /**
100    * {@inheritdoc}
101    */
102   public function setOwner(UserInterface $account) {
103     $this->set('user_id', $account->id());
104     return $this;
105   }
106
107   /**
108    * {@inheritdoc}
109    */
110   public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
111     $fields = parent::baseFieldDefinitions($entity_type);
112     $fields += static::publishedBaseFieldDefinitions($entity_type);
113
114     $fields['name'] = BaseFieldDefinition::create('string')
115       ->setLabel('Name')
116       ->setRevisionable(TRUE)
117       ->setDisplayOptions('view', [
118         'label' => 'hidden',
119         'type' => 'string',
120         'weight' => -5,
121       ]);
122
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
129       // simplify testing.
130       ->setDefaultValue([0 => ['target_id' => 1]])
131       ->setTranslatable(TRUE)
132       ->setDisplayOptions('form', [
133         'type' => 'entity_reference_autocomplete',
134         'weight' => -1,
135         'settings' => [
136           'match_operator' => 'CONTAINS',
137           'size' => '60',
138           'placeholder' => '',
139         ],
140       ]);
141
142     return $fields;
143   }
144
145 }