771d81a9adcc487018e7c06934b908af6be9306c
[yaffs-website] / web / core / modules / system / tests / modules / entity_test / src / Entity / EntityTest.php
1 <?php
2
3 namespace Drupal\entity_test\Entity;
4
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;
11
12 /**
13  * Defines the test entity class.
14  *
15  * @ContentEntityType(
16  *   id = "entity_test",
17  *   label = @Translation("Test entity"),
18  *   handlers = {
19  *     "list_builder" = "Drupal\entity_test\EntityTestListBuilder",
20  *     "view_builder" = "Drupal\entity_test\EntityTestViewBuilder",
21  *     "access" = "Drupal\entity_test\EntityTestAccessControlHandler",
22  *     "form" = {
23  *       "default" = "Drupal\entity_test\EntityTestForm",
24  *       "delete" = "Drupal\entity_test\EntityTestDeleteForm"
25  *     },
26  *     "route_provider" = {
27  *       "html" = "Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider",
28  *     },
29  *     "translation" = "Drupal\content_translation\ContentTranslationHandler",
30  *     "views_data" = "Drupal\entity_test\EntityTestViewsData"
31  *   },
32  *   base_table = "entity_test",
33  *   admin_permission = "administer entity_test content",
34  *   persistent_cache = FALSE,
35  *   list_cache_contexts = { "entity_test_view_grants" },
36  *   entity_keys = {
37  *     "id" = "id",
38  *     "uuid" = "uuid",
39  *     "bundle" = "type",
40  *     "label" = "name",
41  *     "langcode" = "langcode",
42  *   },
43  *   links = {
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}",
48  *   },
49  *   field_ui_base_route = "entity.entity_test.admin_form",
50  * )
51  *
52  * Note that this entity type annotation intentionally omits the "create" link
53  * template. See https://www.drupal.org/node/2293697.
54  */
55 class EntityTest extends ContentEntityBase implements EntityOwnerInterface {
56
57   /**
58    * {@inheritdoc}
59    */
60   public static function preCreate(EntityStorageInterface $storage, array &$values) {
61     parent::preCreate($storage, $values);
62     if (empty($values['type'])) {
63       $values['type'] = $storage->getEntityTypeId();
64     }
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
71     $fields = parent::baseFieldDefinitions($entity_type);
72
73     $fields['name'] = BaseFieldDefinition::create('string')
74       ->setLabel(t('Name'))
75       ->setDescription(t('The name of the test entity.'))
76       ->setTranslatable(TRUE)
77       ->setSetting('max_length', 32)
78       ->setDisplayOptions('view', [
79         'label' => 'hidden',
80         'type' => 'string',
81         'weight' => -5,
82       ])
83       ->setDisplayOptions('form', [
84         'type' => 'string_textfield',
85         'weight' => -5,
86       ]);
87
88     $fields['created'] = BaseFieldDefinition::create('created')
89       ->setLabel(t('Authored on'))
90       ->setDescription(t('Time the entity was created'))
91       ->setTranslatable(TRUE);
92
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
99       // simplify testing.
100       ->setDefaultValue([0 => ['target_id' => 1]])
101       ->setTranslatable(TRUE)
102       ->setDisplayOptions('form', [
103         'type' => 'entity_reference_autocomplete',
104         'weight' => -1,
105         'settings' => [
106           'match_operator' => 'CONTAINS',
107           'size' => '60',
108           'placeholder' => '',
109         ],
110       ]);
111
112     return $fields + \Drupal::state()->get($entity_type->id() . '.additional_base_field_definitions', []);
113   }
114
115   /**
116    * {@inheritdoc}
117    */
118   public function getOwner() {
119     return $this->get('user_id')->entity;
120   }
121
122   /**
123    * {@inheritdoc}
124    */
125   public function getOwnerId() {
126     return $this->get('user_id')->target_id;
127   }
128
129   /**
130    * {@inheritdoc}
131    */
132   public function setOwnerId($uid) {
133     $this->set('user_id', $uid);
134     return $this;
135   }
136
137   /**
138    * {@inheritdoc}
139    */
140   public function setOwner(UserInterface $account) {
141     $this->set('user_id', $account->id());
142     return $this;
143   }
144
145   /**
146    * Sets the name.
147    *
148    * @param string $name
149    *   Name of the entity.
150    *
151    * @return $this
152    */
153   public function setName($name) {
154     $this->set('name', $name);
155     return $this;
156   }
157
158   /**
159    * Returns the name.
160    *
161    * @return string
162    */
163   public function getName() {
164     return $this->get('name')->value;
165   }
166
167   /**
168    * {@inheritdoc}
169    */
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);
176   }
177
178 }