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 }}"),
32 * bundle_label = @Translation("{{ entity_type_label }} type"),
36 * "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
38 * "view_builder" = "Drupal\{{ machine_name }}\{{ class_prefix }}ViewBuilder",
40 * "list_builder" = "Drupal\{{ machine_name }}\{{ class_prefix }}ListBuilder",
41 * "views_data" = "Drupal\views\EntityViewsData",
42 {% if access_controller %}
43 * "access" = "Drupal\{{ machine_name }}\{{ class_prefix }}AccessControlHandler",
46 * "add" = "Drupal\{{ machine_name }}\Form\{{ class_prefix }}Form",
47 * "edit" = "Drupal\{{ machine_name }}\Form\{{ class_prefix }}Form",
48 * "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm"
50 * "route_provider" = {
51 * "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
54 * base_table = "{{ entity_type_id }}",
55 * data_table = "{{ entity_type_id }}_field_data",
57 * revision_table = "{{ entity_type_id }}_revision",
58 * revision_data_table = "{{ entity_type_id }}_field_revision",
59 * show_revision_ui = TRUE,
62 * translatable = TRUE,
64 * admin_permission = "administer {{ entity_type_label|lower }}",
68 * "revision" = "revision_id",
71 * "langcode" = "langcode",
74 * "bundle" = "bundle",
76 * "label" = "{{ title_base_field ? 'title' : 'id' }}",
80 * revision_metadata_keys = {
81 {% if author_base_field %}
82 * "revision_user" = "revision_uid",
84 {% if created_base_field %}
85 * "revision_created" = "revision_timestamp",
87 * "revision_log_message" = "revision_log"
92 * "add-form" = "{{ entity_base_path }}/add/{{ '{' }}{{ entity_type_id }}{{ '_type}' }}",
93 * "add-page" = "{{ entity_base_path }}/add",
95 * "add-form" = "{{ entity_base_path }}/add",
97 * "canonical" = "/{{ entity_type_id }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}",
98 * "edit-form" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/edit",
99 * "delete-form" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/delete",
100 * "collection" = "/admin/content/{{ entity_type_id|u2h }}"
103 * bundle_entity_type = "{{ entity_type_id }}_type",
104 * field_ui_base_route = "entity.{{ entity_type_id }}_type.edit_form"
105 {% elseif fieldable %}
106 * field_ui_base_route = "entity.{{ entity_type_id }}.settings"
110 class {{ class_prefix }} extends {% if revisionable %}Revisionable{% endif %}ContentEntityBase implements {{ class_prefix }}Interface {
112 {% if changed_base_field %}
113 use EntityChangedTrait;
119 * When a new {{ entity_type_label|lower }} entity is created, set the uid entity reference to
120 * the current user as the creator of the entity.
122 public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
123 parent::preCreate($storage_controller, $values);
124 $values += ['uid' => \Drupal::currentUser()->id()];
127 {% if title_base_field %}
131 public function getTitle() {
132 return $this->get('title')->value;
138 public function setTitle($title) {
139 $this->set('title', $title);
144 {% if status_base_field %}
148 public function isEnabled() {
149 return (bool) $this->get('status')->value;
155 public function setStatus($status) {
156 $this->set('status', $status);
161 {% if created_base_field %}
165 public function getCreatedTime() {
166 return $this->get('created')->value;
172 public function setCreatedTime($timestamp) {
173 $this->set('created', $timestamp);
178 {% if author_base_field %}
182 public function getOwner() {
183 return $this->get('uid')->entity;
189 public function getOwnerId() {
190 return $this->get('uid')->target_id;
196 public function setOwnerId($uid) {
197 $this->set('uid', $uid);
204 public function setOwner(UserInterface $account) {
205 $this->set('uid', $account->id());
213 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
215 $fields = parent::baseFieldDefinitions($entity_type);
217 {% if title_base_field %}
218 $fields['title'] = BaseFieldDefinition::create('string')
219 {% if revisionable %}
220 ->setRevisionable(TRUE)
222 {% if translatable %}
223 ->setTranslatable(TRUE)
225 ->setLabel(t('Title'))
226 ->setDescription(t('The title of the {{ entity_type_label|lower }} entity.'))
228 ->setSetting('max_length', 255)
229 ->setDisplayOptions('form', [
230 'type' => 'string_textfield',
233 ->setDisplayConfigurable('form', TRUE)
234 ->setDisplayOptions('view', [
239 ->setDisplayConfigurable('view', TRUE);
242 {% if status_base_field %}
243 $fields['status'] = BaseFieldDefinition::create('boolean')
244 {% if revisionable %}
245 ->setRevisionable(TRUE)
247 ->setLabel(t('Status'))
248 ->setDescription(t('A boolean indicating whether the {{ entity_type_label|lower }} is enabled.'))
249 ->setDefaultValue(TRUE)
250 ->setSetting('on_label', 'Enabled')
251 ->setDisplayOptions('form', [
252 'type' => 'boolean_checkbox',
254 'display_label' => FALSE,
258 ->setDisplayConfigurable('form', TRUE)
259 ->setDisplayOptions('view', [
264 'format' => 'enabled-disabled',
267 ->setDisplayConfigurable('view', TRUE);
270 {% if description_base_field %}
271 $fields['description'] = BaseFieldDefinition::create('text_long')
272 {% if revisionable %}
273 ->setRevisionable(TRUE)
275 {% if translatable %}
276 ->setTranslatable(TRUE)
278 ->setLabel(t('Description'))
279 ->setDescription(t('A description of the {{ entity_type_label|lower }}.'))
280 ->setDisplayOptions('form', [
281 'type' => 'text_textarea',
284 ->setDisplayConfigurable('form', TRUE)
285 ->setDisplayOptions('view', [
286 'type' => 'text_default',
290 ->setDisplayConfigurable('view', TRUE);
293 {% if author_base_field %}
294 $fields['uid'] = BaseFieldDefinition::create('entity_reference')
295 {% if revisionable %}
296 ->setRevisionable(TRUE)
298 {% if translatable %}
299 ->setTranslatable(TRUE)
301 ->setLabel(t('Author'))
302 ->setDescription(t('The user ID of the {{ entity_type_label|lower }} author.'))
303 ->setSetting('target_type', 'user')
304 ->setDisplayOptions('form', [
305 'type' => 'entity_reference_autocomplete',
307 'match_operator' => 'CONTAINS',
313 ->setDisplayConfigurable('form', TRUE)
314 ->setDisplayOptions('view', [
319 ->setDisplayConfigurable('view', TRUE);
322 {% if created_base_field %}
323 $fields['created'] = BaseFieldDefinition::create('created')
324 ->setLabel(t('Authored on'))
325 {% if translatable %}
326 ->setTranslatable(TRUE)
328 ->setDescription(t('The time that the {{ entity_type_label|lower }} was created.'))
329 ->setDisplayOptions('view', [
331 'type' => 'timestamp',
334 ->setDisplayConfigurable('form', TRUE)
335 ->setDisplayOptions('form', [
336 'type' => 'datetime_timestamp',
339 ->setDisplayConfigurable('view', TRUE);
342 {% if changed_base_field %}
343 $fields['changed'] = BaseFieldDefinition::create('changed')
344 ->setLabel(t('Changed'))
345 {% if translatable %}
346 ->setTranslatable(TRUE)
348 ->setDescription(t('The time that the {{ entity_type_label|lower }} was last edited.'));