3 namespace Drupal\entity_test\Entity;
5 use Drupal\Core\Entity\ContentEntityBase;
6 use Drupal\Core\Entity\EntityTypeInterface;
7 use Drupal\Core\Field\BaseFieldDefinition;
8 use Drupal\Core\Entity\EntityStorageInterface;
9 use Drupal\user\EntityOwnerInterface;
10 use Drupal\user\UserInterface;
13 * Defines the test entity class.
17 * label = @Translation("Test entity"),
19 * "list_builder" = "Drupal\entity_test\EntityTestListBuilder",
20 * "view_builder" = "Drupal\entity_test\EntityTestViewBuilder",
21 * "access" = "Drupal\entity_test\EntityTestAccessControlHandler",
23 * "default" = "Drupal\entity_test\EntityTestForm",
24 * "delete" = "Drupal\entity_test\EntityTestDeleteForm"
26 * "route_provider" = {
27 * "html" = "Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider",
29 * "translation" = "Drupal\content_translation\ContentTranslationHandler",
30 * "views_data" = "Drupal\entity_test\EntityTestViewsData"
32 * base_table = "entity_test",
33 * admin_permission = "administer entity_test content",
34 * persistent_cache = FALSE,
35 * list_cache_contexts = { "entity_test_view_grants" },
41 * "langcode" = "langcode",
44 * "canonical" = "/entity_test/{entity_test}",
45 * "add-form" = "/entity_test/add",
46 * "edit-form" = "/entity_test/manage/{entity_test}/edit",
47 * "delete-form" = "/entity_test/delete/entity_test/{entity_test}",
49 * field_ui_base_route = "entity.entity_test.admin_form",
52 class EntityTest extends ContentEntityBase implements EntityOwnerInterface {
57 public static function preCreate(EntityStorageInterface $storage, array &$values) {
58 parent::preCreate($storage, $values);
59 if (empty($values['type'])) {
60 $values['type'] = $storage->getEntityTypeId();
67 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
68 $fields = parent::baseFieldDefinitions($entity_type);
70 $fields['name'] = BaseFieldDefinition::create('string')
72 ->setDescription(t('The name of the test entity.'))
73 ->setTranslatable(TRUE)
74 ->setSetting('max_length', 32)
75 ->setDisplayOptions('view', [
80 ->setDisplayOptions('form', [
81 'type' => 'string_textfield',
85 $fields['created'] = BaseFieldDefinition::create('created')
86 ->setLabel(t('Authored on'))
87 ->setDescription(t('Time the entity was created'))
88 ->setTranslatable(TRUE);
90 $fields['user_id'] = BaseFieldDefinition::create('entity_reference')
91 ->setLabel(t('User ID'))
92 ->setDescription(t('The ID of the associated user.'))
93 ->setSetting('target_type', 'user')
94 ->setSetting('handler', 'default')
95 // Default EntityTest entities to have the root user as the owner, to
97 ->setDefaultValue([0 => ['target_id' => 1]])
98 ->setTranslatable(TRUE)
99 ->setDisplayOptions('form', [
100 'type' => 'entity_reference_autocomplete',
103 'match_operator' => 'CONTAINS',
115 public function getOwner() {
116 return $this->get('user_id')->entity;
122 public function getOwnerId() {
123 return $this->get('user_id')->target_id;
129 public function setOwnerId($uid) {
130 $this->set('user_id', $uid);
137 public function setOwner(UserInterface $account) {
138 $this->set('user_id', $account->id());
145 * @param string $name
146 * Name of the entity.
150 public function setName($name) {
151 $this->set('name', $name);
160 public function getName() {
161 return $this->get('name')->value;