3 namespace Drupal\{{ machine_name }}\Entity;
6 {% if not revisionable %}
7 use Drupal\Core\Entity\ContentEntityBase;
9 use Drupal\Core\Entity\EntityStorageInterface;
10 use Drupal\Core\Entity\EntityTypeInterface;
11 use Drupal\Core\Field\BaseFieldDefinition;
13 use Drupal\Core\Entity\RevisionableContentEntityBase;
15 use Drupal\{{ machine_name }}\{{ class_prefix }}Interface;
16 {% if author_base_field %}
17 use Drupal\user\UserInterface;
19 {% if changed_base_field %}
20 use Drupal\Core\Entity\EntityChangedTrait;
25 * Defines the {{ entity_type_label|lower }} entity class.
28 * id = "{{ entity_type_id }}",
29 * label = @Translation("{{ entity_type_label }}"),
30 * label_collection = @Translation("{{ entity_type_label|plural }}"),
33 * "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
35 * "view_builder" = "Drupal\{{ machine_name }}\{{ class_prefix }}ViewBuilder",
37 * "list_builder" = "Drupal\{{ machine_name }}\{{ class_prefix }}ListBuilder",
38 * "views_data" = "Drupal\views\EntityViewsData",
39 {% if access_controller %}
40 * "access" = "Drupal\{{ machine_name }}\{{ class_prefix }}AccessControlHandler",
43 * "add" = "Drupal\{{ machine_name }}\Form\{{ class_prefix }}Form",
44 * "edit" = "Drupal\{{ machine_name }}\Form\{{ class_prefix }}Form",
45 * "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm"
48 * base_table = "{{ entity_type_id }}",
50 * revision_table = "{{ entity_type_id }}_revision",
51 * show_revision_ui = TRUE,
54 * translatable = TRUE,
56 * admin_permission = "administer {{ entity_type_label|lower }}",
60 * "revision" = "revision_id",
63 * "langcode" = "langcode",
65 * "label" = "{{ title_base_field ? 'title' : 'id' }}",
69 * revision_metadata_keys = {
70 {% if author_base_field %}
71 * "revision_user" = "revision_uid",
73 {% if created_base_field %}
74 * "revision_created" = "revision_timestamp",
76 * "revision_log_message" = "revision_log"
80 * "canonical" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}",
81 * "edit-form" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/edit",
82 * "delete-form" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/delete",
83 * "collection" = "/admin/content/{{ entity_type_id|u2h }}"
86 * field_ui_base_route = "entity.{{ entity_type_id }}.settings"
90 class {{ class_prefix }} extends {% if revisionable %}Revisionable{% endif %}ContentEntityBase implements {{ class_prefix }}Interface {
92 {% if changed_base_field %}
93 use EntityChangedTrait;
99 * When a new {{ entity_type_label|lower }} entity is created, set the uid entity reference to
100 * the current user as the creator of the entity.
102 public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
103 parent::preCreate($storage_controller, $values);
104 $values += ['uid' => \Drupal::currentUser()->id()];
107 {% if title_base_field %}
111 public function getTitle() {
112 return $this->get('title')->value;
118 public function setTitle($title) {
119 $this->set('title', $title);
124 {% if status_base_field %}
128 public function isEnabled() {
129 return (bool) $this->get('status')->value;
135 public function setStatus($status) {
136 $this->set('promote', $status);
141 {% if created_base_field %}
145 public function getCreatedTime() {
146 return $this->get('created')->value;
152 public function setCreatedTime($timestamp) {
153 $this->set('created', $timestamp);
158 {% if author_base_field %}
162 public function getOwner() {
163 return $this->get('uid')->entity;
169 public function getOwnerId() {
170 return $this->get('uid')->target_id;
176 public function setOwnerId($uid) {
177 $this->set('uid', $uid);
184 public function setOwner(UserInterface $account) {
185 $this->set('uid', $account->id());
193 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
195 $fields = parent::baseFieldDefinitions($entity_type);
197 {% if title_base_field %}
198 $fields['title'] = BaseFieldDefinition::create('string')
199 {% if revisionable %}
200 ->setRevisionable(TRUE)
202 {% if translatable %}
203 ->setTranslatable(TRUE)
205 ->setLabel(t('Title'))
206 ->setDescription(t('The title of the {{ entity_type_label|lower }} entity.'))
208 ->setSetting('max_length', 255)
209 ->setDisplayOptions('form', [
210 'type' => 'string_textfield',
213 ->setDisplayConfigurable('form', TRUE)
214 ->setDisplayOptions('view', [
219 ->setDisplayConfigurable('view', TRUE);
222 {% if status_base_field %}
223 $fields['status'] = BaseFieldDefinition::create('boolean')
224 {% if revisionable %}
225 ->setRevisionable(TRUE)
227 ->setLabel(t('Status'))
228 ->setDescription(t('A boolean indicating whether the {{ entity_type_label|lower }} is enabled.'))
229 ->setDefaultValue(TRUE)
230 ->setSetting('on_label', 'Enabled')
231 ->setDisplayOptions('form', [
232 'type' => 'boolean_checkbox',
234 'display_label' => FALSE,
238 ->setDisplayConfigurable('form', TRUE)
239 ->setDisplayOptions('view', [
244 'format' => 'enabled-disabled',
247 ->setDisplayConfigurable('view', TRUE);
250 {% if description_base_field %}
251 $fields['description'] = BaseFieldDefinition::create('text_long')
252 {% if revisionable %}
253 ->setRevisionable(TRUE)
255 {% if translatable %}
256 ->setTranslatable(TRUE)
258 ->setLabel(t('Description'))
259 ->setDescription(t('A description of the {{ entity_type_label|lower }}.'))
260 ->setDisplayOptions('form', [
261 'type' => 'text_textarea',
264 ->setDisplayConfigurable('form', TRUE)
265 ->setDisplayOptions('view', [
266 'type' => 'text_default',
270 ->setDisplayConfigurable('view', TRUE);
273 {% if author_base_field %}
274 $fields['uid'] = BaseFieldDefinition::create('entity_reference')
275 {% if revisionable %}
276 ->setRevisionable(TRUE)
278 {% if translatable %}
279 ->setTranslatable(TRUE)
281 ->setLabel(t('Author'))
282 ->setDescription(t('The user ID of the {{ entity_type_label|lower }} author.'))
283 ->setSetting('target_type', 'user')
284 ->setDisplayOptions('form', [
285 'type' => 'entity_reference_autocomplete',
287 'match_operator' => 'CONTAINS',
293 ->setDisplayConfigurable('form', TRUE)
294 ->setDisplayOptions('view', [
299 ->setDisplayConfigurable('view', TRUE);
302 {% if created_base_field %}
303 $fields['created'] = BaseFieldDefinition::create('created')
304 ->setLabel(t('Authored on'))
305 {% if translatable %}
306 ->setTranslatable(TRUE)
308 ->setDescription(t('The time that the {{ entity_type_label|lower }} was created.'))
309 ->setDisplayOptions('view', [
311 'type' => 'timestamp',
314 ->setDisplayConfigurable('form', TRUE)
315 ->setDisplayOptions('form', [
316 'type' => 'datetime_timestamp',
319 ->setDisplayConfigurable('view', TRUE);
322 {% if changed_base_field %}
323 $fields['changed'] = BaseFieldDefinition::create('changed')
324 ->setLabel(t('Changed'))
325 {% if translatable %}
326 ->setTranslatable(TRUE)
328 ->setDescription(t('The time that the {{ entity_type_label|lower }} was last edited.'));