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 * Note that this entity type annotation intentionally omits the "create" link
53 * template. See https://www.drupal.org/node/2293697.
55 class EntityTest extends ContentEntityBase implements EntityOwnerInterface {
60 public static function preCreate(EntityStorageInterface $storage, array &$values) {
61 parent::preCreate($storage, $values);
62 if (empty($values['type'])) {
63 $values['type'] = $storage->getEntityTypeId();
70 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
71 $fields = parent::baseFieldDefinitions($entity_type);
73 $fields['name'] = BaseFieldDefinition::create('string')
75 ->setDescription(t('The name of the test entity.'))
76 ->setTranslatable(TRUE)
77 ->setSetting('max_length', 32)
78 ->setDisplayOptions('view', [
83 ->setDisplayOptions('form', [
84 'type' => 'string_textfield',
88 $fields['created'] = BaseFieldDefinition::create('created')
89 ->setLabel(t('Authored on'))
90 ->setDescription(t('Time the entity was created'))
91 ->setTranslatable(TRUE);
93 $fields['user_id'] = BaseFieldDefinition::create('entity_reference')
94 ->setLabel(t('User ID'))
95 ->setDescription(t('The ID of the associated user.'))
96 ->setSetting('target_type', 'user')
97 ->setSetting('handler', 'default')
98 // Default EntityTest entities to have the root user as the owner, to
100 ->setDefaultValue([0 => ['target_id' => 1]])
101 ->setTranslatable(TRUE)
102 ->setDisplayOptions('form', [
103 'type' => 'entity_reference_autocomplete',
106 'match_operator' => 'CONTAINS',
112 return $fields + \Drupal::state()->get($entity_type->id() . '.additional_base_field_definitions', []);
118 public function getOwner() {
119 return $this->get('user_id')->entity;
125 public function getOwnerId() {
126 return $this->get('user_id')->target_id;
132 public function setOwnerId($uid) {
133 $this->set('user_id', $uid);
140 public function setOwner(UserInterface $account) {
141 $this->set('user_id', $account->id());
148 * @param string $name
149 * Name of the entity.
153 public function setName($name) {
154 $this->set('name', $name);
163 public function getName() {
164 return $this->get('name')->value;
170 public function getEntityKey($key) {
171 // Typically this protected method is used internally by entity classes and
172 // exposed publicly through more specific getter methods. So that test cases
173 // are able to set and access entity keys dynamically, update the visibility
174 // of this method to public.
175 return parent::getEntityKey($key);